VALUES 是 INSER 命令的子句。
INSERT INOT 表名(列名1,列名2,…) VALUES(值1,值2,…)
--语法:--SELECT * FROM (--VALUES-- (1,2,3,......)-- ,(1,2,3,......)-- ,(1,2,3,......)-- ,(1,2,3,......)-- ,(1,2,3,......)-- ,(1,2,3,......)--) AS t(c1,c2,c3......)SELECT * FROM ( VALUES (123456),(123456),(123456),(123456),(123456)) AS T(c1)
试检1
-- 试检1SELECT * FROM ( VALUES (123456),(123456),(123456),(123456),(123456)) AS T(c1)
试检2
-- 试检2 生成1个列的临时表select * into #tmpTable FROM ( VALUES (123456),(123456),(123456),(123456),(123456)) AS T(c1)
试检3
-- 生成5个列的临时表 #tmpTable2select * into #tmpTable2 FROM ( VALUES (1,2,3,4,123456) ,(2,3,4,5,123456) ,(3,4,5,6,123456) ,(4,100,100,3,123456) ,(23,4,5,5,123456)) AS T(c1,c2,c3,c4,c5)select * from #tmpTable2
成功了高级一点的例子
计算5列的平均值,求各,最大值,最小值,计数
使用values子句,将每行数据构造为只有一个字段的表,以后求最大值,非常巧妙
-- 计算5列的平均值,求各,最大值,最小值,计数select * into #tmpTable3 FROM ( VALUES (1,2,3,4,123456) ,(2,3,40000,5000,123456) ,(3,4222,5,6000,123456) ,(4,100,100,3,123456) ,(23,4,5,5,123456)) AS T(c1,c2,c3,c4,c5)select a.* ,(select Max(NewData) from (values (C1),(C2),(C3),(C4),(C5)) as #temp(NewData)) as nMax ,(select Min(NewData) from (values (C1),(C2),(C3),(C4),(C5)) as #temp(NewData)) as nMai ,(select Sum(NewData) from (values (C1),(C2),(C3),(C4),(C5)) as #temp(NewData)) as nSum ,(select Avg(NewData) from (values (C1),(C2),(C3),(C4),(C5)) as #temp(NewData)) as nAvg ,(select count(NewData) from (values (C1),(C2),(C3),(C4),(C5)) as #temp(NewData)) as nCount from #tmpTable3 as a
到此这篇关于SQL 使用 VALUES 生成带数据的临时表实例代码详解的文章就介绍到这了,更多相关sql 临时表内容请搜索武林网以前的文章或继续浏览下面的相关文章希望大家以后多多支持武林网!
新闻热点
疑难解答