Home >Database >Mysql Tutorial >SQL 特殊语句(学习笔记)

SQL 特殊语句(学习笔记)

WBOY
WBOYOriginal
2016-06-07 18:05:391180browse

SQL 特殊语句(学习笔记),以XML 返回,处理大批量数据的添加,转换类型等实现

【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联表删除数据 deletefrom Souce from Souce inner join Student on Souce.sid = Student.id
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn