在存在语句中使用表变量

发布于 2024-08-06 21:32:33 字数 427 浏览 9 评论 0原文

我试图根据条件更新表变量内的列,条件是表变量的 ID 不存在于不同的表中:

DECLARE @BugRep TABLE(BugCode VARCHAR(50),DevFirstName VARCHAR(50), DevLastName    VARCHAR(50), BugDate VARCHAR(20), IsValid VARCHAR(1))

UPDATE @BugRep 
SET IsValid = 'N' WHERE NOT EXISTS(SELECT * FROM BUG b WHERE @BugRep.BUGCODE = b.CODE)

当我尝试编译包含这些语句的过程时,我得到一个“必须声明标量变量“@BugRep”消息。

我如何在 NOT EXISTS 子句中使用表变量?

我正在使用 SQL Server 2008

I am trying to update a column inside of a table variable based on a condition, the condition being that the ID of the table variable does not exist in a different table:

DECLARE @BugRep TABLE(BugCode VARCHAR(50),DevFirstName VARCHAR(50), DevLastName    VARCHAR(50), BugDate VARCHAR(20), IsValid VARCHAR(1))

UPDATE @BugRep 
SET IsValid = 'N' WHERE NOT EXISTS(SELECT * FROM BUG b WHERE @BugRep.BUGCODE = b.CODE)

When i try to compile the procedure that has these statements, I get a "Must declare the scalar variable "@BugRep" message.

How do i go about using the table variable inside of the NOT EXISTS clause?

I am using SQL Server 2008

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

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

发布评论

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

评论(3

心是晴朗的。 2024-08-13 21:32:33

这会起作用:

[@BugRep].BUGCODE

顺便说一下,您还需要将“b.CODE”更改为“b.BUGCODE”;)

This will work:

[@BugRep].BUGCODE

You'll also need to change "b.CODE" to "b.BUGCODE" by the way ;)

执笏见 2024-08-13 21:32:33

这实际上是非常挑剔的。使用 womp 的建议查看下面的内嵌注释,并尝试使用 LEFT OUTER JOIN。

CREATE TABLE Bug (CODE VARCHAR(50))

DECLARE @BugRep TABLE (
    BugCode         VARCHAR(50),
    --DevFirstName    VARCHAR(50),
    --DevLastName     VARCHAR(50),
    --BugDate         VARCHAR(20),
    IsValid         CHAR(1)
)

INSERT INTO Bug (CODE) VALUES ('Code1'), ('Code2'), ('Code3')

INSERT INTO @BugRep (BugCode) VALUES ('Code1'), ('Code2'), ('Code4')

SELECT CODE FROM Bug ORDER BY CODE
SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

UPDATE @BugRep                          -- Can't be [@BugRep] ("Invalid object name '@BugRep'.")
SET IsValid = 'N'
WHERE NOT EXISTS (
    SELECT *
    FROM BUG b
    WHERE [@BugRep].BUGCODE = b.CODE    -- Can't be @BugRep ("Must declare the scalar variable "@BugRep".")
)

SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

UPDATE @BugRep                          -- Can be either @BugRep or [@BugRep]
SET IsValid = 'Y'
FROM @BugRep                            -- Can't be [@BugRep] ("Invalid object name '@BugRep'.")
LEFT OUTER JOIN BUG
ON [@BugRep].BUGCODE = BUG.CODE         -- Can't be @BugRep ("Must declare the scalar variable "@BugRep".")
WHERE BUG.CODE IS NOT NULL

SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

DROP TABLE Bug
GO

This is actually very picky. Check out the in-line comments below using womp's suggestion and also trying a LEFT OUTER JOIN.

CREATE TABLE Bug (CODE VARCHAR(50))

DECLARE @BugRep TABLE (
    BugCode         VARCHAR(50),
    --DevFirstName    VARCHAR(50),
    --DevLastName     VARCHAR(50),
    --BugDate         VARCHAR(20),
    IsValid         CHAR(1)
)

INSERT INTO Bug (CODE) VALUES ('Code1'), ('Code2'), ('Code3')

INSERT INTO @BugRep (BugCode) VALUES ('Code1'), ('Code2'), ('Code4')

SELECT CODE FROM Bug ORDER BY CODE
SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

UPDATE @BugRep                          -- Can't be [@BugRep] ("Invalid object name '@BugRep'.")
SET IsValid = 'N'
WHERE NOT EXISTS (
    SELECT *
    FROM BUG b
    WHERE [@BugRep].BUGCODE = b.CODE    -- Can't be @BugRep ("Must declare the scalar variable "@BugRep".")
)

SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

UPDATE @BugRep                          -- Can be either @BugRep or [@BugRep]
SET IsValid = 'Y'
FROM @BugRep                            -- Can't be [@BugRep] ("Invalid object name '@BugRep'.")
LEFT OUTER JOIN BUG
ON [@BugRep].BUGCODE = BUG.CODE         -- Can't be @BugRep ("Must declare the scalar variable "@BugRep".")
WHERE BUG.CODE IS NOT NULL

SELECT BugCode, IsValid FROM @BugRep ORDER BY BugCode

DROP TABLE Bug
GO
七堇年 2024-08-13 21:32:33

这是前两个版本使用别名来解决您的问题:

UPDATE @BugRep
SET IsValid = 'N'
FROM @BugRep BR
    LEFT JOIN BUG B
        ON BR.BUGCode = B.CODE
WHERE B.CODE is null

这也避免了与“不为空”和“不存在”相关的低效率。

Here's a version of the previous two using aliases to get around your issue:

UPDATE @BugRep
SET IsValid = 'N'
FROM @BugRep BR
    LEFT JOIN BUG B
        ON BR.BUGCode = B.CODE
WHERE B.CODE is null

This also avoids the inefficiencies related to "is not null" and "not exists".

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