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

最新文章

  1. 从程序员到CTO的Java技术路线图(我爱分享)
  2. 介绍两种风格的URL
  3. [Architecture Design] 累进式Domain Layer
  4. hdu 2767 Proving Equivalences
  5. Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
  6. easyui 分页实现
  7. UVA10304---(区间DP)
  8. 纠错《COM技术内幕》之ProgID
  9. git 使用系列(二)---- 分支和合并
  10. Linux指令--wc
  11. 【BZOJ2004】公交线路(动态规划,状态压缩,矩阵快速幂)
  12. 深度学习之Batch Normalization
  13. javascript面向对象习题答案
  14. AES加解密程序的实现
  15. Postgresql之VACUUM和VACUUM FULL对比
  16. ImageView控件有关问题
  17. JAVA基础知识总结:二十
  18. 我的TDD实践---CI持续集成
  19. Windows Sysinternals实战指南
  20. 【SqlServer】T-SQL的简介及基本用法

热门文章

  1. .NET开源项目一览
  2. scikit-learn:3.5. Validation curves: plotting scores to evaluate models
  3. (转)C/C++ 宏详解
  4. 关于Tomcat的启动
  5. C++之易混淆知识点二
  6. js中深拷贝代码实现
  7. SLAM概念学习之随机SLAM算法
  8. FCC高级编程篇之Make a Person
  9. WCF客户端获取服务端异常[自定义异常]
  10. 使用VUE开发微信小程序