http://vyaskn.tripod.com/differences_between_set_and_select.htm

https://stackoverflow.com/questions/3945361/set-versus-select-when-assigning-variables

  • SET is the ANSI standard for variable assignment, SELECT is not.
  • SET can only assign one variable at a time, SELECT can make multiple assignments at once.
  • If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)
  • When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from its previous value)
  • As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.

http://www.sql-server-helper.com/tips/set-vs-select-assigning-variables.aspx

SET SELECT
ANSI standard for variable assignment. Non-ANSI standard when assigning variables.
Can only assign one variable at a time.

SET @Index = 1
SET @LoopCount = 10
SET @InitialValue = 5
Can assign values to more than one variable at a time.

SELECT @Index = 1, @LoopCount = 10,
@InitialValue = 5
When assigning from a query and the query returns no result, SET will assign a NULL value to the variable.

DECLARE @CustomerID NCHAR(5)
SET @CustomerID = 'XYZ'
SET @CustomerID = (SELECT [CustomerID]
FROM [dbo].[Customers]
WHERE [CustomerID] = 'ABC')
SELECT @CustomerID -– Returns NULL
When assigning from a query and the query returns no result, SELECT will not make the assignment and therefore not change the value of the variable.

DECLARE @CustomerID NCHAR(5)
SET @CustomerID = 'XYZ'
SELECT @CustomerID = [CustomerID]
FROM [dbo].[Customers]
WHERE [CustomerID] = 'ABC'
SELECT @CustomerID –- Returns XYZ
When assigning from a query that returns more than one value, SET will fail with an error.

SET = (SELECT [CustomerID]
FROM [dbo].[Customers]) Msg 512, Level 16, State 1, Line 3
Subquery returned more than 1 value.
This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the
subquery is used as an expression.
When assigning from a query that returns more than one value, SELECT will assign the last value returned by the query and hide the fact that the query returned more than one row.

SELECT  @CustomerID = [CustomerID]
FROM [dbo].[Customers]
-- No error generated

What is the difference between SET and SELECT when assigning values to variables, in T-SQL?的更多相关文章

  1. Laravel - Union + Paginate at the same time? and another problem----1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from

    ### 这是这几天,碰到的一个比较头疼的问题 使用union all联合查询,同时laravel 生成分页,但发生报错? QueryException : SQLSTATE The used from ...

  2. 【转】The difference between categorical(Nominal ), ordinal and interval variables

    What is the difference between categorical, ordinal and interval variables? In talking about variabl ...

  3. using the library to generate a dynamic SELECT or DELETE statement mysqlbaits xml配置文件 与 sql构造器 对比

    https://github.com/mybatis/mybatis-dynamic-sql MyBatis Dynamic SQL     What Is This? This library is ...

  4. SELECT 1,2,3...的含义及其在SQL注入中的用法

    首先,select 之后可以接一串数字:1,2,3-只是一个例子,这串数字并不一定要按从小到大排列,也不一定从1开始,这串数字的值和顺序是任意的,甚至可以是重复的,如:11,465,7461,35 或 ...

  5. select count(1) from table where ..这句sql语句的作用

    作用是计算一共有多少符合条件的行.1并不是表示第一个字段,而是表示一个固定值,count(1)和count(2)效果是一样的 count(*),执行时会把星号翻译成字段的具体名字,效果也是一样的,不过 ...

  6. select rows by values in a column from Dataframe

    df.loc[df['column_name'] == some_value] details in: http://stackoverflow.com/questions/17071871/sele ...

  7. Oracle数据库学习笔记

    创建表的同时插入数据:create table zhang3 as select * from zhang1;create table zhang3(id,name) as select * from ...

  8. MySQL: @variable vs. variable. Whats the difference?

    MySQL: @variable vs. variable. Whats the difference?   up vote351down votefavorite 121 In another qu ...

  9. LINQ to SQL Select查询

    1. 查询所有字段 using (NorthwindEntities context = new NorthwindEntities()) { var order = from n in contex ...

随机推荐

  1. 开心的小明(南阳oj49)(01背包)

    开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 小明今天非常开心.家里购置的新房就要领钥匙了,新房里有一间他自己专用的非常宽敞的房间.更让他高兴的是,妈妈 ...

  2. Unity学习笔记 之 发射小球碰撞物体的代码记录

    绑定在摄像机上的脚本 using UnityEngine; using System.Collections; public class abc : MonoBehaviour { //设置移动速度 ...

  3. dpdk l2fwd 应用流程分析

    int MAIN(int argc, char **argv) { struct lcore_queue_conf *qconf; struct rte_eth_dev_info dev_info; ...

  4. Bootstrap全局CSS样式之表单

    .form-control--将单独的表单控件赋予一些全局样式,如默认宽度width:100%. .form-group--包裹表单控件,获得最好的排列: .form-inline--将表单设置为内联 ...

  5. 如何让Java写的程序,脱离Eclipse在别人的电脑上运行?

    Java程序运行,离不开JRE. 将JRE文件夹拷贝出来, 将编写程序的bin文件夹拷贝出来, 再编写一个bat文件. start jre\bin\javaw.exe -cp .\bin\ -Djav ...

  6. http协议无状态中的 "状态" 到底指的是什么?!(转载)

    转载自:https://www.cnblogs.com/bellkosmos/p/5237146.html   引子: 最近在好好了解http,发现对介绍http的第一句话[http协议是无状态的,无 ...

  7. Linux命令locate

    centos安装locate命令 centos6.3刚初始化安装完毕,有个配置文件不知道存在什么地方,想用locate命令来查找下,发现系统提示,找不到该命令.以前经常用的命令为什么找不到了呢???原 ...

  8. WPF学习(二) - 绑定

    绑定,这个看起来很神奇的东西,于我这种喜欢刨根儿的人而言,理解起来非常困难.    WPF绑定的核心思想是:数据层属性值的改变,能反应给展示层,反之亦然,并且这个响应的过程能被分离出来. 传统Winf ...

  9. (转载) 清理缓存 IPackageStatsObserver

    清理缓存 IPackageStatsObserver 2016-04-10 13:40 2288人阅读 评论(0) 收藏 举报  分类: android(59)  版权声明:本文为博主原创文章,未经博 ...

  10. vmware workstation pro 14 虚拟机无法开启、黑屏的解决方案汇总

    方案1:卸载鲁大师,重启. 方案2:管理员命令行,输入netsh winsock reset,重启. 方案3:360安全管家修复LSP,重启. 方案4:卸载14.0,安装12.0,手动导入虚拟机.