Re: which is better... SET or SELECT?

From: Roji. P. Thomas (lazydragon_at_nowhere.com)
Date: 08/02/04


Date: Mon, 2 Aug 2004 18:10:52 +0530

One more thing to be careful is, if the SELCT includes an Aggreagate
function,
it returns NULL istead of zero

DECLARE @id INTEGER

SET @id = 0

SELECT @id = Max(id)
 FROM sysobjects
 WHERE name = 'does not exist'

SELECT @id -- Returns NULL

DECLARE @id INTEGER

SET @id = 0

SELECT @id = id
 FROM sysobjects
 WHERE name = 'does not exist'

SELECT @id -- Returns Zero

-- 
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:ObednZbR7r0elZPcRVn-uA@giganews.com...
> One important difference between SET and SELECT for variable assignment is
> the behaviour when no rows are returned from the SELECT statement. If
> variables are assigned in the SELECT then the value of the variables will
> remain unchanged. Example:
>
> DECLARE @id INTEGER
>
> SET @id = 0
>
> SELECT @id = id
>  FROM sysobjects
>  WHERE name = 'does not exist'
>
> SELECT @id -- Returns Zero
>
> SET @id=
>  (SELECT id
>   FROM sysobjects
>   WHERE name = 'does not exist')
>
> SELECT @id -- Returns NULL
>
> -- 
> David Portas
> SQL Server MVP
> --
>
>


Relevant Pages

  • Re: which is better... SET or SELECT?
    ... > One important difference between SET and SELECT for variable assignment is> the behaviour when no rows are returned from the SELECT statement. ... > DECLARE @id INTEGER ... > FROM sysobjects ... > SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: coding problem using salford FTN95
    ... values less than zero make no physical sense here. ... the code does not declare the types of any of its variables. ... the compilers implicit typing so that you will get an error message ... The program statement is supposed to ...
    (comp.lang.fortran)
  • Re: Help
    ... I don't think you need to join from sysobjects just try ... select object_name,* from syscomments where ctext like '%order by ... > declare @tbnames varchar ... > fetch next from test into @tbnames ...
    (microsoft.public.sqlserver.server)
  • Re: Base
    ... Moreover, if you only want to worry about typical machines, then the values ... but that would fail if the range of G_Int didn't include zero. ... Slightly less sloppy programmers would declare the counter as having the ...
    (comp.lang.ada)
  • Re: Deleting all SPROCS and UDFs
    ... DECLARE @PARENT VARCHAR ... FETCH NEXT FROM DROP_CURSOR INTO @PROCNAME ... DECLARE DROP_CURSOR CURSOR FOR select name from sysobjects where type='FN' ...
    (microsoft.public.sqlserver.server)