300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > SQL Server 中 with tmp 临时表的用法

SQL Server 中 with tmp 临时表的用法

时间:2021-05-22 01:00:55

相关推荐

SQL Server 中 with tmp 临时表的用法

SQL Server 中 with tmp 临时表的用法----------with临时表用法,有时候采用临时表比采用in的效率更高,避免了全表扫描。实例中实现了查询普通题、大题、子题目的sql-----------(对于大题套小题,采用先查小题信息再反查大题相关信息的方式)一、简答1. 会计师的定义?a。AAAAb。BBBBc。CCCCd。DDDD二、判断2.下面那个是对的?a。对b。错三、简答题3. 看一段文字描述,回答每个小题目:<1>、第一个小题目a。AAAAb。BBBBc。CCCCd。DDDD<2>、第二个小题目a。AAAAb。BBBBc。CCCCd。DDDD------------with tmp as (select distinct parentIDfrom QZ_SIM_PAPER_QUESTIONWHERE simPaperID = 205and isDel <> 1and ansResult = -1and queType <> 4and parentID <> 0)select * from(SELECT t.*FROM QZ_SIM_PAPER_QUESTION t,tmpWHERE (t.questionID = tmp.parentID or t.parentID = tmp.parentID) and t.simPaperID = 205) tmp2union all (SELECT *FROM QZ_SIM_PAPER_QUESTIONWHERE isDel <> 1 and queType <> 4 and parentID = 0 and simPaperID = 205 andansResult = -1) order by parentID,queType在使用CTE时应注意如下几点:1. CTE后面必须直接跟使用CTE的SQL语句(如select、insert、update等),否则,CTE将失效;withcr as(select CountryRegionCode from person.CountryRegion where Name like 'C%')select * from person.CountryRegion -- 应将这条SQL语句去掉-- 使用CTE的SQL语句应紧跟在相关的CTE后面--select * from person.StateProvince where CountryRegionCode in (select * from cr)2. CTE后面也可以跟其他的CTE,但只能使用一个with,多个CTE中间用逗号(,)分隔;withcte1 as(select * from table1 where name like 'abc%'),cte2 as(select * from table2 where id > 20),cte3 as(select * from table3 where price < 100)select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id3. 如果CTE的表达式名称与某个数据表或视图重名,则紧跟在该CTE后面的SQL语句使用的仍然是CTE,当然,后面的SQL语句使用的就是数据表或视图了;-- table1是一个实际存在的表withtable1 as(select * from persons where age < 30)select * from table1 -- 使用了名为table1的公共表表达式select * from table1 -- 使用了名为table1的数据表4. CTE 可以引用自身,也可以引用在同一WITH 子句中预先定义的CTE。不允许前向引用。5. 不能在CTE_query_definition 中使用以下子句:(1)COMPUTE 或COMPUTE BY(2)ORDER BY(除非指定了TOP 子句)(3)INTO(4)带有查询提示的OPTION 子句(5)FOR XML(6)FOR BROWSE6. 如果将CTE 用在属于批处理的一部分的语句中,那么在它之前的语句必须以分号结尾。declare @s nvarchar(3)set @s = 'C%'; -- 必须加分号witht_tree as(select CountryRegionCode from person.CountryRegion where Name like @s)select * from person.StateProvince where CountryRegionCode in (select * from t_tree)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。