SQL SET 语句

发布于 2024-11-17 17:32:24 字数 349 浏览 3 评论 0原文

我目前正在提高我的 SQL 知识。目前我正在尝试通过从 select 语句获取值来声明变量。第一个问题:这可能吗?

第二个问题:我有这个 SQL 尝试执行上述操作。我的意图是将 @version_group 设置为 version_replace 所包含的任何内容,这始终是单行、单列结果。

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

我怎样才能将其纠正为有效的语法? (假设有可能)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

软甜啾 2024-11-24 17:32:24

我怎样才能将其更正为有效
合成? (假设这是可能的)

您想要的语法如下,它需要一条您在最初的工作中没有的信息(FROM 子句):

DECLARE @version_group int

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

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
柳若烟 2024-11-24 17:32:24

这是有可能的。就这样做(SQL 2008):

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

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

declare @version_group as int=
(SELECT version_replace 
FROM users
WHERE id=@sid);
零崎曲识 2024-11-24 17:32:24
DECLARE @version_group int

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

不要忘记包含您的数据源(即表、视图,替换上面的 MyVersionTable)。

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

贪恋 2024-11-24 17:32:24
  1. 当然
  2. SELECT @version_group = version_replace FROM YourTable WHERE id=@sid
  1. Sure
  2. SELECT @version_group = version_replace FROM YourTable WHERE id=@sid
谎言 2024-11-24 17:32:24
DECLARE @version_group int

-- THEN

SET @version_group = 1

-- OR!

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

-- THEN

SET @version_group = 1

-- OR!

SELECT @version_group FROM version_replace WHERE id=@sid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文