【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