DDR爱好者之家 Design By 杰米

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

-- 试检1
SELECT * 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个列的临时表 #tmpTable2
select * 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
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米