zoukankan      html  css  js  c++  java
  • (4.47)sql server 中的 values 构造临时表

    【1】构造临时表的几种办法

    【1.1】CTE

    我们一般构造临时表,都要用CTE递归(但这比较适合大数据量)

    我们要小数据量构造临时表,我以前经常用的是 

    ;with as temp(
    select 1 as id1 ,2 as id2 union all
    select 3 as id1 ,4 as id2 
    )
    select * from temp

    【1.2】真实生成临时表( select * into from table )

    select 1 as id1 ,2 as id2 into #temp1 
    union all
    select 3 as id1 ,4 as id2 
    
    select * from #temp1

    【1.3】VALUES

    VALUES 用得最多,最常见的就是 

    INSER INOT 表名(列名1,列名2,......) VALUES(值1,值2,......)

    ----------------------------------------------------------------------------

    另外一种用法:

    使用 values 返回临时表。

    ----------------------------------------------------------------------------

    --语法:
    
    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(1,1),
        (1,2) 
    ) t(id1,id2)

      

    ----------------------------------------

    需要注意地方:列数量 要与  ” Values(.....)” ,小括号中的列的数量一样,并且“()”是作为一组数据,也就是一行数据,所以,每个小括号里面的列数量也要一样。

    其实还有其他办法,比如派生表  select * from (select a from table where a<100)......

    这里就不一一举例了,把常用的拿出来就OK拉。

  • 相关阅读:
    centos6 Cacti部署文档
    nginx 3.nginx+fastcgi
    nginx 2.基本配置
    nginx 1.安装
    mongodb入门教程二
    mongodb入门教程
    一款jQuery立体感动态下拉导航菜单特效
    一款jQuery仿海尔官网全屏焦点图特效代码
    一款非常炫酷的jQuery动态随机背景滚动特效
    一款jquery编写图文下拉二级导航菜单特效
  • 原文地址:https://www.cnblogs.com/gered/p/13535012.html
Copyright © 2011-2022 走看看