提问人:SkyeBoniwell 提问时间:8/16/2023 最后编辑:Mark RotteveelSkyeBoniwell 更新时间:9/14/2023 访问量:85
我带有 2 个 INSERT 命令的 SQL 语句在第二个 INSERT 上失败
My SQL statement with 2 INSERT commands is failing on the second INSERT
问:
我有以下两点说法:INSERT
-- Create a new database entry for the astronomicalObject in the GradObservers database
-- based on values of the given id
INSERT INTO astro.bodies (id, title, coordinates)
SELECT NEXT VALUE FOR bodies.nextid, title, 9291
FROM astro.astro
WHERE id = 002811
-- Insert new id from table above into astro.sources
-- Insert other fields from existing bodyId 002811
INSERT INTO astro.sources(bodyId, baseValue, Url)
(SELECT CONVERT(BIGINT, current_value) FROM sys.sequences WHERE schema_id = 71)
SELECT baseValue, Url
FROM astro.sources
WHERE bodyId = 002811
我正在尝试执行 2 个命令,其中第二个命令取决于第一个命令的值。INSERT
ID
第一个使用数据库中的 a 来获取它的 .INSERT
SEQUENCE
ID
然后第二个也需要使用该 ID。INSERT
第一个有效,但我从第二个语句中得到一个错误:INSERT
INSERT 语句的选择列表包含的项少于插入列表。SELECT 值的数目必须与 INSERT 列数匹配。
有没有办法做到这一点?
答:
2赞
Charlieface
8/16/2023
#1
要插入一个值,后跟一个单独的语句。SELECT
正确的语法是将 嵌套在 main 中。为了安全起见,您可能应该使用序列名称。SELECT
SELECT
INSERT INTO astro.sources
(bodyId, baseValue, Url)
SELECT
(
SELECT CONVERT(BIGINT, seq.current_value)
FROM sys.sequences seq
JOIN sys.schemas sch ON sch.schema_id = seq.schema_id
WHERE sch.name = 'bodies'
AND seq.name = 'nextid'
),
baseValue,
Url
FROM astro.sources
WHERE bodyId = 002811;
但是,最好首先将其存储在变量中。
DECLARE @id bigint = NEXT VALUE FOR bodies.nextid;
INSERT INTO astro.bodies
(id, title, coordinates)
SELECT
@id,
title,
9291
FROM astro.astro
WHERE id = 2811;
INSERT INTO astro.sources
(bodyId, baseValue, Url)
SELECT
@id,
baseValue,
Url
FROM astro.sources
WHERE bodyId = 2811;
旁注:没有多大意义。 是一个数字而不是一个字符串,那么为什么它有前导零。bodyId = 002811
002811
评论
INSERT INTO astro.sources(bodyId, baseValue, Url) select(SELECT CONVERT(BIGINT, current_value) FROM sys.sequences WHERE schema_id = 71), baseValue, Url FROM astro.sources WHERE bodyId = 002811
OUTPUT
INSERT
SEQUENCE