sql - 使用 from 子句插入到输出表中
问题描述
我正在尝试复制表中的一些行,并更改了几个值,并且我还需要存储一个旧的(将丢失的)ID 以便稍后进行进一步处理。我正在尝试使用输出子句来存储该信息,但 SQL Server 抛出以下错误:
Msg 4104 <...> The multi-part identifier could not be bound.
这是我正在复制数据的表(稍作修改以减少列数):
Create Table Elements
(
id int Identity(0,1) not null, --PK
name varchar(50) not null,
modelID int not null, --FK
constraint PK_Elements primary key (id)
);
这是我的查询:
declare @outputTable table
(
oldElementID int,
id int,
name varchar(50),
modelID bigint
);
Insert into Elements
(name, modelID)
Output e.id as oldElementID,
Inserted.id,
Inserted.name,
Inserted.modelID into @outputTable
select e.name, @newModelID
from Elements as e
where e.modelID = @oldModelID
注意:@oldModelID 和@newModelID 是先前声明和设置的。
我不确定我的逻辑是否错误,我必须采取不同的方法(但我确信可以这样做)。或者,如果我只是有一个我无法完全解决的错误。
任何帮助,将不胜感激。
谢谢!
解决方案
我重新创建了这样的问题:
CREATE TABLE #a (a INT, b INT)
INSERT INTO #a (a,b) VALUES (42, 43)
INSERT INTO #a (a, b)
OUTPUT a.a, a.b, inserted.a, inserted.b
SELECT a.b, a.a
FROM #a a
插入操作产生消息:
Msg 4104, Level 16, State 1, Line 7
The multi-part identifier "a.a" could not be bound.
Msg 4104, Level 16, State 1, Line 7
The multi-part identifier "a.b" could not be bound.
那是因为 INSERT 命令看不到我在 select 命令中使用的别名“a”。
推荐阅读
- gis - 将大星星对象转换为 sf 对象
- docker - dockerized conan 显示 FileExistsError: [Errno 17] 文件存在:'./util-linux-2.33.1/tests/expected/libmount/context-X-mount.mkdir'
- karate -
我正在使用下面的 XML,我需要在 XML 中为下面的标签设置 10 次值。
- 投票号码
而且我还想用 10 个更新的新 Po
- python - 如何使 for 循环在特定值处停止
- python - 在终端本地捕获和过滤所有日志
- python - 如何使用 DFS 在这里工作的这个 python 代码以获得最大深度?
- c# - 连接到 rev api 网络核心
- laravel - 如何用惯性js和laravel 8.0上传文件
- pandas - Pandas DataFrame TypeError:“类型”对象不可下标
- ruby-on-rails - 如何用值破坏模型