如何在“update from select”中使用表变量询问?
我有这个表变量声明,后跟一个查询:
DECLARE @CurrentItems TABLE
(
ItemId uniqueidentifier,
ItemUnits int
)
UPDATE U SET U.Units = U.Units + [@CurrentItems].ItemUnits
FROM @CurrentItems CI INNER JOIN U ON U.UId=CI.ItemId;
并且 U
定义如下:
CREATE TABLE [dbo].[U] (
[UId] UNIQUEIDENTIFIER UNIQUE NOT NULL,
[Units] INT DEFAULT ((0)) NOT NULL
);
当我在 SQL Management Studio 中针对 SQL Server 2005 Express 运行它时,我得到以下结果:
消息 208,级别 16,状态 1,第 24 行
对象名称“@CurrentItems”无效。
我已经浏览过这个和this 非常相似的问题,但无法弄清楚如何解决问题。
实际问题是什么?我该如何解决?
I have this table variable declaration followed by a query:
DECLARE @CurrentItems TABLE
(
ItemId uniqueidentifier,
ItemUnits int
)
UPDATE U SET U.Units = U.Units + [@CurrentItems].ItemUnits
FROM @CurrentItems CI INNER JOIN U ON U.UId=CI.ItemId;
And U
is defined as follows:
CREATE TABLE [dbo].[U] (
[UId] UNIQUEIDENTIFIER UNIQUE NOT NULL,
[Units] INT DEFAULT ((0)) NOT NULL
);
When I run that in SQL Management Studio against SQL Server 2005 Express I get the following:
Msg 208, Level 16, State 1, Line 24
Invalid object name '@CurrentItems'.
I've already looked through this and this very similar questions but can't figure out how to solve the problem.
What's the actual problem and how do I resolve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将
@CurrentItems
与CI
建立别名,因此只需使用CI
:另请查看您的查询,您是否有类似
U 的内容。 UId = CU.ItemID
。什么是CU?您已经使用 CI 为 @CurrentItems 起了一个别名,那么 CU 的用途是什么?如果这是一个错误,只是一个拼写错误,请确保将任何对CU
的引用更改为CI
。您也没有告诉我们
U
是什么,我希望这是一个有效的表。You've aliased
@CurrentItems
withCI
so just useCI
:Also take a look at your query you have something like
U.UId = CU.ItemID
. What is CU? You've made an alias for @CurrentItems with CI, so what is the purpose of CU? If this is a mistake, just a typo make sure you change any reference toCU
withCI
.You also don't tell us what
U
is, I hope this is a valid table.