Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SET statement

Tags:

sql

select

set

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)

like image 799
YsoL8 Avatar asked Feb 23 '23 18:02

YsoL8


2 Answers

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
like image 75
heisenberg Avatar answered Mar 05 '23 14:03

heisenberg


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

declare @version_group as int=
(SELECT version_replace 
FROM users
WHERE id=@sid);
like image 45
sara Avatar answered Mar 05 '23 14:03

sara