MSSQL虚拟表


1.#表名
存储在硬盘中,用完需删除。

select * into #cpgk from A 
...
drop table #cpgk--用完即删

  1. @
    存储在内存中,效率快。
declare @classid int =(select classid from Subject where SubjectName=@subjectName) --查询当前科目属于那一个班级
set @subjectId=(select SubjectId from Subject where SubjectName=@subjectName) --获取科目ID
declare @totalCount int --总人数 :那一个班级需要考试这一科目
set @totalCount=(select COUNT(*) from Student where ClassId=@classid) print @totalcount  --14
declare @unpassNum int --不及格人数

3.with as
同样存储在内存中,效率快,但是只能用一次。

...
	;with t as (select DISTINCT workcenterid,GDYH,GDY,CJDD,JHRQ,YS,CQTS,FS,FJ from #tmpls ) 
	insert into #tmpwjdd(hztj,workcenterid,gdyh,gdy,ysgs)
	select '生技经理',workcenterid,gdyh,gdy,sum(isnull(fj,0))
	FROM t 
	group by workcenterid,gdyh,gdy
...

虚拟表可以处理一些复杂的功能,但是为什么建议少用呢?