WITH common_table_expression (Transact-SQL)

Specifies a temporary named result set, known as a common table expression (CTE).

This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.

This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement.

A common table expression can include references to itself.

This is referred to as a recursive common table expression.

-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse  

[ WITH <common_table_expression> [ ,...n ] ]  

<common_table_expression>::=
expression_name [ ( column_name [ ,...n ] ) ]
AS
( CTE_query_definition )

Remarks

A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE statement that references some or all the CTE columns.

A CTE can also be specified in a CREATE VIEW statement as part of the defining SELECT statement of the view.

Error

Incorrect syntax near the keyword 'with'.

If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

https://stackoverflow.com/questions/1439123/incorrect-syntax-near-the-keyword-with-previous-statement-must-be-terminated

When to use with clause in sql

The WITH keyword is used to create a temporary named result set. These are called Common Table Expressions.

A very basic, self-explanatory example:

WITH Administrators (Name, Surname)
AS
(
SELECT Name, Surname FROM Users WHERE AccessRights = 'Admin'
)
SELECT * FROM Administrators

For further reading and more examples, I suggest starting out with the following MSDN article:

WITH common_table_expression (Transact-SQL)的更多相关文章

  1. T-SQL、JET SQL、PL-SQL

    数据库分为网状.层状.关系.对象四种类型,目前的数据库一般都是属于关系数据库(包括MYSQL和SQL SERVER),网状.层状基本上已成历史,对象类型尚未普及. SQL 语言是有 ANSI 标准的. ...

  2. SQL SERVER 简介及应用 - 数据库系统原理

    SQL SERVER 是一个分布式的关系型数据库管理系统(RDBMS),具有客户 - 服务器体系结构,一般发行的版本有企业版.标准版.个人版.开发版. SQL SERVER 提供的服务 MS SQL ...

  3. SQL Server中的锁 详解 nolock,rowlock,tablock,xlock,paglock

    摘自: http://www.myexception.cn/sql-server/385562.html 高手进 锁 nolock,rowlock,tablock,xlock,paglock 锁 no ...

  4. SQL SERVER CURSOR游标的使用(转载)

    一:认识游标 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据 ...

  5. SQL Server数据库学习总结

    经过一段时间的学习,也对数据库有了一些认识,数据库基本是由表,关系,操作组成:对于初学者首先要学的     一图胜“十”言:SQL Server 数据库总结 一个大概的总结 经过一段时间的学习,也对数 ...

  6. SQL Server自定义函数( 转载于51CTO )

    用户自定义函数自定义函数不能执行一系列改变数据库状态的操作,可以像系统函数在查询或存储过程等的程序中使用,也可以像相信过程一样能过 execute 命令来执行.自定义函数中存储了一个 Transact ...

  7. SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)

    1.引言 T-SQL(Transact Structured Query Language)是标准的SQL的扩展,是程序和SQL Server沟通的主要语言. T-SQL语言主要由以下几部分组成: 数 ...

  8. 在SQL Server中实现关系模型的阶梯到级别3的t -SQL DML

    在SQL Server中实现关系模型的阶梯到级别3的t -SQL DML 格雷戈里·拉森(Gregory Larsen),2017/08/02(第一次出版:2011 /11/09) 原文链接:http ...

  9. Python和SQL 2017的强大功能

    Python和SQL Server 2017的强大功能   原文来自:https://www.red-gate.com/simple-talk/sql/sql-development/power-py ...

  10. Linux 上的 SQL Server 2017 的安装指南

    一:介绍背景 微软在2016年 3 月首次对外宣布了 Linux 版的 SQL Server,并于2017年 7 月发布了首个公开 RC 版.前几日在美国奥兰多召开的微软 Ignite 2017 大会 ...

随机推荐

  1. [CTSC2016]单调上升路径

    题目:UOJ#201. 题目大意:给定n个点(n是偶数)的完全图,现在要你给每条边确定一个权值(互不相等),使得最长的单调上升路径最短.现在要你输出边的权值. 一条路径被称为单调上升的,如果沿着它走时 ...

  2. java+jsp+sqlserver实现简单的增删改查操作 连接数据库代码

    1,网站系统开发需要掌握的技术 (1)网页设计语言,html语言css语言等 (2)Java语言 (3)数据库 (4)等 2,源程序代码 (1) 连接数据库代码 package com.jaovo.m ...

  3. Docker学习总结(11)——八个Docker的真实应用场景

    [编者的话]Flux 7介绍了常用的8个Docker的真实使用场景,分别是简化配置.代码流水线管理.提高开发效率.隔离应用.整合服务器.调试能力.多租户环境.快速部署.我们一直在谈Docker,Doc ...

  4. [terry笔记]data guard基础知识

    如下介绍了data guard的基础知识,整理自网络: Data Gurad 通过冗余数据来提供数据保护,Data Gurad 通过日志同步机制保证冗余数据和主数据之前的同步,这种同步可以是实时,延时 ...

  5. 2015 Multi-University Training Contest 2 Friends

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. 死锁的Dump文件

    死锁的Dump文件 package com.stono.thread; public class DeadLockDemo { private static String A = "A&qu ...

  7. Cocos2dx 小技巧(十五)话说ScrollView的delegate实现过程

    附:本文參加了CSDN博客大赛.亲假设认为这篇文章不错,就大胆的来投上一票吧! !!http://vote.blog.csdn.net/Article/Details? articleid=34140 ...

  8. CSDN博客2014年4月24日清理缓存

    亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...

  9. 编写SDR SDRAM页突发模式控制器的注意点

    网上有很多的SDR SDRAM控制器的代码,但都是基于burst1/2/4/8模式下的,这种模式下传输高速的相机数据还是有点拮据的,所以花了几天把这些模式改造成了页突发模式.我的这个控制器模型是这样的 ...

  10. Logical Operators (Transact-SQL)

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/logical-operators-transact-sql Logical ...