When to use SET and SELECT?

18

I know that as in practically all issues related to "one or the other" there should be situations where it is best to use SET or SELECT , in a procedure (for example).

At work we have the habit of always using SELECT , even if to assign value to only one variable, eg:

@declare @variavel varchar(2)
select @variavel = 'ok'

Is this the best practice or is it a simple assignment it would be best to use SET ? Is there any performance / memory advantage in using SELECT in situations like this?

    
asked by anonymous 29.09.2017 / 19:32

2 answers

26

I've always had this doubt, but I've never researched it and even this happens a lot with me. In the StackOVerflow in English has this topic explaining when to use both the main differences are:

  • SET is ANSI standard for assigning variables, SELECT is not.
  • SET can only assign one variable at a time, SELECT can do several assignments at the same time.
  • If you assign from a query, SET can only assign a value climb. If the query returns multiple values / rows then SET an error may occur. SELECT will assign one of the values to the variable e will hide the fact that several values have been returned (then, probably would never know why something was wrong in another place)
  • When assigning from a query if there is no value returned SET will assign NULL , where SELECT will not do the assignment (so the variable will not be changed from its previous value). See code below
  • Regarding differences in performance there are no differences between SET and SELECT . The ability to SELECT making multiple assignments in a single action gives a slight advantage performance on SET .
  • Translating code item 3:

    Test yourself using the code below.

    declare @var varchar(20)
    set @var = 'Joe'
    set @var = (select Campo from SuaTabela)
    select @var 
    
    select @var = Campo from SuaTabela
    select @var
    

    The first code will return the following error message:

      

    Subquery returned more than 1 value. This is not permitted when the   subquery follows =,! =, & lt ;, < =, & gt ;, > = or when the subquery is used as   an expression.

    The second will return you a value.

    Translating code item 4:

    declare @var varchar(20)
    set @var = 'Joe'
    set @var = (select name from master.sys.tables where name = 'qwerty')
    select @var /* @var Agora é null */
    --SAÍDA NULL
    
    set @var = 'Joe'
    select @var = name from master.sys.tables where name = 'qwerty' -- AQUI NÃO É ATRIBUÍDO NULL
    select @var
    --SAÍDA Joe
    

    It is very common to use the system in which I work SELECT in FUNCTIONS to concatenate comma-separated values instead of displaying one per line.

    In practice

    Imagine that I want to know all of your customers' emails, and they should have been comma-separated in a single result.

    A FUNCTION that would solve your problem will look something like:

    CREATE FUNCTION [dbo].[fncEmails](@IDCliente int)
    RETURNS VARCHAR(MAX)
    AS
    BEGIN
        DECLARE @RETORNO VARCHAR(MAX)
    
            SELECT @retorno = COALESCE(@retorno + ', ', '') + Email
            FROM Clientes 
       RETURN @retorno
    END
    

    Result:

    '[email protected], [email protected], [email protected]'
    

    SQLFiddle

        
    29.09.2017 / 19:37
    0

    By working with mysql , I followed the following practices in sql server :

    • I use set to assign value to control variables;
    • I use select for all other cases.

    In other words, when I need to initialize a variable that is used to control a loop , for example, I use set (ex set @contador = 0 ); when the assignment depends on any query the base, I use select (ex select @contador = count(*) from usuarios ). Since the performance difference is practically nonexistent, there are no problems in the execution.

    Note that you could use set even with queries to the base (ex set @contador = (select count(*) from usuarios) ), but I believe it will be more complex to read.

        
    24.10.2018 / 16:25