dbTalk Databases Forums  

general question

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss general question in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
MikeJ
 
Posts: n/a

Default general question - 07-26-2007 , 12:59 PM






hi...i would like to know best practice when assigning a value to a variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ




Reply With Quote
  #2  
Old   
Jason Lepack
 
Posts: n/a

Default Re: general question - 07-26-2007 , 02:17 PM






http://ryanfarley.com/blog/archive/2004/03/01/390.aspx

On Jul 26, 1:59 pm, "MikeJ" <vettes_n_j... (AT) sbcglobal (DOT) net> wrote:
Quote:
hi...i would like to know best practice when assigning a value to a variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ



Reply With Quote
  #3  
Old   
Roy Harvey
 
Posts: n/a

Default Re: general question - 07-26-2007 , 02:35 PM



If I had to choose just one it would have to be SELECT simply because
there are times SET will not do the job.

One more point that I did not see coverd in the article linked by
Jason. It is common to want to retrieve both the @@error and
@@rowcount values resulting from a command. To get both and save them
to @variables (commonly @error and @rowcount) you MUST use SELECT, as
the @@values are destroyed by the first command that retrieves them.

I use SET to assign a constant, or perhaps increment a counter, but
use SELECT when the data comes from a query or is otherwise more
complicated. But I don't pretend that is "best practice", as I don't
believe there is a clear best here.

Roy Harvey
Beacon Falls, CT

On Thu, 26 Jul 2007 10:59:17 -0700, "MikeJ"
<vettes_n_jets (AT) sbcglobal (DOT) net> wrote:

Quote:
hi...i would like to know best practice when assigning a value to a variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ



Reply With Quote
  #4  
Old   
MikeJ
 
Posts: n/a

Default Re: general question - 07-26-2007 , 05:11 PM



thanks for your response
MJ

"Roy Harvey" <roy_harvey (AT) snet (DOT) net> wrote

Quote:
If I had to choose just one it would have to be SELECT simply because
there are times SET will not do the job.

One more point that I did not see coverd in the article linked by
Jason. It is common to want to retrieve both the @@error and
@@rowcount values resulting from a command. To get both and save them
to @variables (commonly @error and @rowcount) you MUST use SELECT, as
the @@values are destroyed by the first command that retrieves them.

I use SET to assign a constant, or perhaps increment a counter, but
use SELECT when the data comes from a query or is otherwise more
complicated. But I don't pretend that is "best practice", as I don't
believe there is a clear best here.

Roy Harvey
Beacon Falls, CT

On Thu, 26 Jul 2007 10:59:17 -0700, "MikeJ"
vettes_n_jets (AT) sbcglobal (DOT) net> wrote:

hi...i would like to know best practice when assigning a value to a
variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ





Reply With Quote
  #5  
Old   
--CELKO--
 
Posts: n/a

Default Re: general question - 08-01-2007 , 05:43 PM



Quote:
best practice when assigning a value to a variable
SET is the syntax used in the SQL/PSM Standard. And Standard SQL has
always had a FROM clause with a SELECT.

However SQL Server does not allow row constructors, so you cannot do
things like:

SET (a, b, c) = (1, 2, 3);
SET (a, b, c) = (SELECT x, y, z FROM Foobar WHERE ..);

as soon as they fix that, use SET.



Reply With Quote
  #6  
Old   
Alex Kuznetsov
 
Posts: n/a

Default Re: general question - 08-01-2007 , 07:46 PM



On Jul 26, 12:59 pm, "MikeJ" <vettes_n_j... (AT) sbcglobal (DOT) net> wrote:
Quote:
hi...i would like to know best practice when assigning a value to a variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ
Mike,

If you need to do many assignments at once, a single assignment

SELECT @var1 = @value1,
(snip)
@var9 = @value9

may run faster than

SET @var1 = @value1
(snip)
SET @var9 = @value9

Verify that. I did benchmarks on 2000 and noticed a difference, but
did not repeat on 2005.

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/



Reply With Quote
  #7  
Old   
MikeJ
 
Posts: n/a

Default Re: general question - 08-06-2007 , 02:02 PM



thanks alot for you responses
MJ

"Alex Kuznetsov" <AK_TIREDOFSPAM (AT) hotmail (DOT) COM> wrote

Quote:
On Jul 26, 12:59 pm, "MikeJ" <vettes_n_j... (AT) sbcglobal (DOT) net> wrote:
hi...i would like to know best practice when assigning a value to a
variable
example below

set
or
select

declare @var1 varchar(25)
set @var1='abc'
select @var1='abc'

to me
set is implicit
select implies conditions

i see both used all over the place here at my new company....
so witch is better practice

tks
MJ

Mike,

If you need to do many assignments at once, a single assignment

SELECT @var1 = @value1,
(snip)
@var9 = @value9

may run faster than

SET @var1 = @value1
(snip)
SET @var9 = @value9

Verify that. I did benchmarks on 2000 and noticed a difference, but
did not repeat on 2005.

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.