数据库教程:SQL 特殊语句(学习笔记)

【1】 以xml 返回 (1)未定义属性的 select logisticsid,logisticsname from logisticscompany for xml a

【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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/617762.html

(0)
上一篇 2021年5月21日
下一篇 2021年5月21日

精彩推荐