【1】 以XML 返回
(1)未定義屬性的
select logisticsId,logisticsName from LogisticsCompany for xml auto,elements
(2)定義屬性節(jié)的
復(fù)制代碼 代碼如下:
SELECT TOP 21 AS Tag,
NULL AS Parent,
title_id AS [titles!1!title_id],
title AS [titles!1!title!element],
type AS [titles!1!type]
FROM
titles
FOR XML EXPLICIT
【2】處理大批量數(shù)據(jù)的添加
復(fù)制代碼 代碼如下:
CREAT PROCEDURE [dbo].[Sp_DB_InsertBatch]
(
@LogXml NVARCHAR(MAX),
@LastTime DATETIME,
@CurrentTime DATETIME
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
--使用OPENXML()需要一個準(zhǔn)備動作:
--定義一個文檔指針@XMLHandler,類型為int(SP_XML_PREPAREDOCUMENT)。
--使用完后還需要把它釋放掉(SP_XML_REMOVEDOCUMENT)
DECLARE @XMLHandler INT;
--OPENXML()本身返回一個行集,并且需要3個參數(shù)和一組列映射定義。
--預(yù)先準(zhǔn)備好的文檔指針@XMLHandler,搜索路徑起點,映射標(biāo)志。
EXEC SP_XML_PREPAREDOCUMENT @XMLHandler OUTPUT,@LogXml;
INSERT INTO [dbo].[DBRate]
([RateID]
,[LastModifyTime]
,[LastTime]
,[CurrentTime])
SELECT [RateID]
,[LastModifyTime]
,@LastTime
,@CurrentTime
FROM OPENXML(@XMLHandler,'/DB/DBRate',2)
WITH( RateID INT
,LastModifyTime DATETIME
)
--釋放掉(SP_XML_REMOVEDOCUMENT)
EXEC SP_XML_REMOVEDOCUMENT @XMLHandler;
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
【3】轉(zhuǎn)換類型
復(fù)制代碼 代碼如下:
select convert(int,'100.1')
select cast('100' as int)
截取并替換其他字符
select stuff('1347097110',5,6,'*******')
獲取ID為4附近的四條數(shù)據(jù)
select top 4 * from Student order by abs(4-id)
分組獲取 select * from (select Row_Number() Over(partition by sex order by ID) as rowindex,* from Student) a where rowindex5
聯(lián)表刪除數(shù)據(jù) deletefrom Souce from Souce inner join Student on Souce.sid = Student.id