开发者

SQL SET statement

开发者 https://www.devze.com 2023-03-15 21:36 出处:网络
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?

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)


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


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

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


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).


  1. Sure
  2. SELECT @version_group = version_replace FROM YourTable WHERE id=@sid


DECLARE @version_group int

-- THEN

SET @version_group = 1

-- OR!

SELECT @version_group FROM version_replace WHERE id=@sid
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号