1、临时表:

1.1)实例1

if(OBJECT_ID('tempdb..#a') IS NOT NULL) drop table #a;
if(OBJECT_ID('tempdb..#b') IS NOT NULL) drop table #b;
SELECT name into #a from syscolumns a with(nolock) where id=OBJECT_ID('NewsLetterSystem_Subscriber');
SELECT name into #b from syscolumns b where id=OBJECT_ID('tmpContact_130828005535769_5243_f0b7');
select * from #a,#b
where #a.name=#b.name;

1.2) 实例2

if(OBJECT_ID('tempdb..#a') IS NOT NULL) drop table #a;
select * into #a from Categories;
select * from #a;

2、公用表:

2.1)实例1

with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
) select * from person.StateProvince where CountryRegionCode in (select * from cr) --其中cr是一个公用表表达式,该表达式在使用上与表变量类似

2.2) CTE后面必须直接跟使用CTE的SQL语句(如select、insert、update等),否则,CTE将失效。如下面的SQL语句将无法正常使用CTE:

with
cr 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.3)CTE后面也可以跟其他的CTE,但只能使用一个with,多个CTE中间用逗号(,)分隔,如下面的SQL语句所示:

with
cte1 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.id

2.4)实例4

with ta as (
SELECT name from syscolumns a with(nolock) where id=OBJECT_ID('NewsLetterSystem_Subscriber')
),
tb as
(
SELECT name from syscolumns b where id=OBJECT_ID('tmpContact_130828005535769_5243_f0b7')
)
select * from ta,tb where ta.name=tb.name

ps:将一个库中数据插到另一个库中:

语句:

insert into b.dbo.b
select * from a.dbo.a

实例1:

insert into dbname1.dbo.tabname1
select * from dbname2.dbo.tabname2

判断临时表是否存在sql:

if(OBJECT_ID('tempdb..#c') is not null) drop table #c
if(OBJECT_ID('tempdb..#e') is not null) drop table #e
if(OBJECT_ID('tempdb..#newc') is not null) drop table #newc

MSSQL 临时表和公用表使用案例的更多相关文章

  1. sql server中的临时表、表变量和公用表表达式

    在编写T-SQL语句的时候,SQL Server提供了三种方法临时存储某些结果集,分别是临时表.表变量和公用表表达式. 临时表 临时表需要在临时数据库TempDB中通过I/O操作来创建表结构,一旦用户 ...

  2. SQL Server中公用表表达式 CTE 递归的生成帮助数据,以及递归的典型应用

    本文出处:http://www.cnblogs.com/wy123/p/5960825.html 我们在做开发的时候,有时候会需要一些帮助数据,必须需要连续的数字,连续间隔的时间点,连续的季度日期等等 ...

  3. 公用表表达式(CTE)递归的生成帮助数据

    本文的作者辛苦了,版权问题特声明本文出处:http://www.cnblogs.com/wy123/p/5960825.html 工作有时候会需要一些帮助数据,必须需要连续的数字,连续间隔的时间点,连 ...

  4. SQL.WITH AS.公用表表达式(CTE)(转)

    一.WITH AS的含义    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...

  5. SQL.WITH AS.公用表表达式(CTE)

    一.WITH AS的含义    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...

  6. SQL中使用WITH AS提高性能,使用公用表表达式(CTE)简化嵌套SQL

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...

  7. T-SQL 公用表表达式(CTE)

    公用表表达式(CTE) 在编写T-SQL代码时,往往需要临时存储某些结果集.前面我们已经广泛使用和介绍了两种临时存储结果集的方法:临时表和表变量.除此之外,还可以使用公用表表达式的方法.公用表表达式( ...

  8. SQL中使用WITH AS提高性能-使用公用表表达式(CTE)简化嵌套SQL

    转:http://wudataoge.blog.163.com/blog/static/80073886200961652022389/ 一.WITH AS的含义     WITH AS短语,也叫做子 ...

  9. [转]SQL中使用WITH AS提高性能-使用公用表表达式(CTE)简化嵌套SQL

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...

随机推荐

  1. Golang Kernel For Jupyter-NoteBook

    上篇回顾:VSCode and NoteBook for JavaScript 正常流程 安装Go语言:sudo apt install golang 安装内核的相关依赖包:sudo apt inst ...

  2. A1109. Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  3. B1016. 部分A+B

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  4. 【洛谷P2257】YY的GCD

    题目大意:有 \(T\) 个询问,每个询问给定 \(N, M\),求 \(1\le x\le N, 1\le y\le M\) 且 \(gcd(x, y)\) 为质数的 \((x, y)\) 有多少对 ...

  5. Pandas库中的DataFrame

    1 简介 DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表. 或许说它可能有点像matlab的矩阵,但是matlab的矩阵只能放数值型值(当然matla ...

  6. java 网络编程 TCP协议 java 服务器和客户端 java socket编程

    一个 HelloWord 级别的 Java Socket 通信的例子.通讯过程:        先启动 Server 端,进入一个死循环以便一直监听某端口是否有连接请求.然后运行 Client 端,客 ...

  7. CentOS7 yum 安装 PHP 5.6.24

    配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...

  8. Vue computed属性

    computed vs methods 我们可以使用Vue中的method计算出学科的总分,最终得到的总数结果是相同的. 在上例的基础上,我们把computed区块中的totalMarks函数整体移到 ...

  9. 字符设备驱动(六)按键poll机制

    title: 字符设备驱动(六)按键poll机制 tags: linux date: 2018-11-23 18:57:40 toc: true --- 字符设备驱动(六)按键poll机制 引入 在字 ...

  10. XML异常

    1.com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效 ...