zoukankan      html  css  js  c++  java
  • What is the difference between SET and SELECT when assigning values to variables, in T-SQL?

    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
    
    
    
    
    
    
  • 相关阅读:
    bzoj 1030 [JSOI2007]文本生成器
    Swift 学习笔记 (闭包)
    Swift 学习笔记 (函数)
    HTML 学习笔记 JQueryUI(Interactions,Widgets)
    HTML 学习笔记 JQuery(表单,表格 操作)
    HTML 学习笔记 JQuery(animation)
    HTML 学习笔记 JQuery(盒子操作)
    HTML 学习笔记 JQuery(事件)
    HTML 学习笔记 JQuery(DOM 操作3)
    HTML 学习笔记 JQuery(DOM 操作2)
  • 原文地址:https://www.cnblogs.com/chucklu/p/7442966.html
Copyright © 2011-2022 走看看