Results 1 to 5 of 5

Thread: [RESOLVED] Set or Select?

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Resolved [RESOLVED] Set or Select?

    On the MSDN site, I came across this statement:

    For assigning variables, we recommend that you use SET @local_variable instead of SELECT @local_variable.
    It doesn't give any context or reason for this. Does anyone know why that's the recommended syntax? It just seems a little clumsier to write:
    Code:
    Set @variable = (SELECT Value FROM Table)
    ... rather than:
    Code:
    Select @variable = Value FROM Table
    Any opinions gratefully received...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Set or Select?

    It's because the set will throw an error if there is more than eligible value returned by the query. Select will simply assign the first eligible value (which will be random unless you're specifying an order). I agree with you that select looks more elegant but set is more reliable.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Set or Select?

    Thanks, my funky friend. That makes sense.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Set or Select?

    I typically use the select because 1) I generally have a where clause because I'm after a specific row, and 2) Rarely am I getting just one field, more often than not, I need a few fields.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: [RESOLVED] Set or Select?

    True, if you're doing multiple values then the select is certainly more efficient but you do need to be careful that the table design doesn't change and make the where clause non-unique - I've been caught that way before. It's yet another of those circumstances where there isn't an answer that's always correct. I personally favour the Set unless there's a pressing reason (ie multiple values in a sproc that needs to be very efficient) not to but it's horses for courses.

    BTW, there is a third option which is to use select with a bracketed sub query. Something like:-
    Code:
    Select @MyVal = (Select myVal From myTable),
      @MyOtherVal = (Select anotherVal From aDifferentTable)
    This will behave in the same way as set in that it will error if more than one value is returned but does have the advantage that you can set several values as part of a single query. Because they sub selects will be evaluated individually this is unlikely to give you any advantage over a series individual Set statements if you're putting it in a sproc but if you're issuing the statements from a client app it could save you a few server trips.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width