SQL SET statement

10,785

Solution 1

How can I correct this to valid syntex? (assuming it's possible)

The syntax you want is as follows, it needs one piece of info that you don't have in your original effort though (the FROM clause) :

DECLARE @version_group int

select @version_group = version_replace from (you're missing this from your query) where id=@sid

Solution 2

It's possible. Just do (SQL 2008):

declare @version_group as int=
(SELECT version_replace 
FROM users
WHERE id=@sid);

Solution 3

DECLARE @version_group int

SELECT @version_group = version_replace
FROM   MyVersionTable
WHERE  id=@sid

Don't forget to include your data source (i.e. table, view, replacing MyVersionTable above).

Share:
10,785
YsoL8
Author by

YsoL8

I am a PHP Developer using CSS, Flash (actionscript 2) and some Javascript for presention. I am actively seeking to expand my knowledge of web development. Also I am actively seeking a job, so contact me if I could be suitable.

Updated on July 28, 2022

Comments

  • YsoL8
    YsoL8 over 1 year

    I am currently improving my knowledge of SQL. Currently I am trying to declare a variable by getting a value from a select statement. First Question: Is this possible?

    Second Question: I have this SQL attempting to do the above. My intension is to set @version_group to whatever version_replace holds, which is always a single row, single column result.

        DECLARE @version_group int
        SET @version_group = SELECT version_replace FROM users WHERE id=@sid
    

    How can I correct this to valid syntax? (assuming it's possible)

  • Alex K.
    Alex K. almost 13 years
    +1 but worth noting its valid in SQL 2k8 only (and the AS is optional)
  • sara
    sara almost 13 years
    I guess you are right :( I work with SQL 2k8 and this is the syntax I use.
  • YsoL8
    YsoL8 almost 13 years
    tick for being first! (I need multiple ticks)