首页 > 代码库 > sql 表变量的使用

sql 表变量的使用

最近第一次使用sql表变量,感觉挺好用,案例如下:

 

  declare @aaa table(Com_SiteGroupID uniqueidentifier)  --定义表变量

 

  ---往表变量里写数据

insert into @aaa select Com_SiteGroupID from Com_SiteGroup where Com_StructureID =@whereId
union all
select a.Com_SiteBasicID from Com_SiteBasic a inner join Com_SiteGroup b on
a.Com_SiteGroupID=b.Com_SiteGroupID where b.Com_StructureID=@whereId

--读取表变量的数据

insert into #temp select c.ObjectText,count(*) as CountNum from Sip_HiddenDisposalInfo a
inner join Sc_InspectionContent b
on a.Sc_InspectionContentID=b.Sc_InspectionContentId inner join Sc_InspectionItem c
on b.Sc_InspectionItemId=c.Sc_InspectionItemId where c.CheckLevel=2
and a.SiteID in (select * from @aaa)
and a.CreateTime>=@firtTime and a.CreateTime<=@lastTime group by c.ObjectText

sql 表变量的使用