【1】 以XML 返回
(1)未定義屬性的
select logisticsId,logisticsName from LogisticsCompany for xml auto,elements
(2)定義屬性節的
復制代碼 代碼如下:
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】處理大批量數據的添加
復制代碼 代碼如下:
CREAT PROCEDURE [dbo].[Sp_DB_InsertBatch]
(
@LogXml NVARCHAR(MAX),
@LastTime DATETIME,
@CurrentTime DATETIME
)
AS
BEGIN
BEGIN TRAN
BEGIN TRY
--使用OPENXML()需要一個准備動作:
--定義一個文檔指針@XMLHandler,類型為int(SP_XML_PREPAREDOCUMENT)。
--使用完後還需要把它釋放掉(SP_XML_REMOVEDOCUMENT)
DECLARE @XMLHandler INT;
--OPENXML()本身返回一個行集,並且需要3個參數和一組列映射定義。
--預先准備好的文檔指針@XMLHandler,搜索路徑起點,映射標志。
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】轉換類型
復制代碼 代碼如下:
select convert(int,'100.1')
select cast('100' as int)
截取並替換其他字符
select stuff('1347097110',5,6,'*******')
獲取ID為4附近的四條數據
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 rowindex<5
聯表刪除數據 deletefrom Souce from Souce inner join Student on Souce.sid = Student.id