子查询在 SQL SErver 2008 中返回了超过 1 个值
Subquery returned more than 1 value in SQL SErver 2008
请看我之前的问题,我仍然坚持。
我有 4 列需要处理,我不能删除 table 来将第 4 列从 int not null 更改为
我是从这个原始陈述中被引导的:
INSERT into ADVNET.dbo.KenCatItemTest
(categoryitemid,itemid,categoryid)
SELECT NEWID(),itemid,'0FCA508F-7EB5-4C2E-8803-DE688C4126E5'
FROM janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0
并抛出错误:
无法将值 NULL 插入列 'LineSequence'、table 'ADVNET.dbo.KenCatItemTest';列不允许空值。插入失败。
声明已终止。
所以现在我正在看这个声明:
DECLARE @CategoryItemId uniqueidentifier;
SET @CategoryItemId = NEWID();
DECLARE @ItemID uniqueidentifier;
SET @ItemId = (select itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
DECLARE @CategoryID uniqueidentifier;
SET @CategoryID = '0FCA508F-7EB5-4C2E-8803-DE688C4126E5';
DECLARE @LineSequence int;
SELECT @LineSequence = ISNULL(MAX(LineSequence),0) + 1
FROM KenCatItemTest WHERE CategoryId = @CategoryId;
INSERT INTO ADVNET.dbo.KenCatItemTest
(CategoryItemId, ItemId, CategoryId, LineSequence)
VALUES (@CategoryItemId, @ItemId, @CategoryId, @LineSequence)
这个新语句抛出错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 515, Level 16, State 2, Line 5
Cannot insert the value NULL into column 'ItemId', table 'ADVNET.dbo.KenCatItemTest'; column does not allow nulls. INSERT fails.
The statement has been terminated.
此语句导致问题:
SET @ItemId = (select itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
如果满足条件的行不止一行,则会发生错误,因为 SET
语句期望在右侧返回标量值而不是行集。
要解决此问题,您可以将标准缩小到只返回一行的位置,或者执行类似提取第一个值的操作。
例如:
SET @ItemId = (select TOP 1 itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
你不能只使用 Row_number() 来为 LineSequence 生成一个值吗?
INSERT INTO ADVNET.dbo.KenCatItemTest(
categoryitemid
,itemid
,categoryid
,LineSequence)
SELECT
NEWID()
,itemid
,'0FCA508F-7EB5-4C2E-8803-DE688C4126E5'
,ROW_NUMBER() OVER(ORDER BY ItemId)
FROM
janel.dbo.item i
WHERE
i.itemnumber LIKE 'c-%'
AND listprice > 0
请看我之前的问题,我仍然坚持。
我有 4 列需要处理,我不能删除 table 来将第 4 列从 int not null 更改为
我是从这个原始陈述中被引导的:
INSERT into ADVNET.dbo.KenCatItemTest
(categoryitemid,itemid,categoryid)
SELECT NEWID(),itemid,'0FCA508F-7EB5-4C2E-8803-DE688C4126E5'
FROM janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0
并抛出错误:
无法将值 NULL 插入列 'LineSequence'、table 'ADVNET.dbo.KenCatItemTest';列不允许空值。插入失败。 声明已终止。
所以现在我正在看这个声明:
DECLARE @CategoryItemId uniqueidentifier;
SET @CategoryItemId = NEWID();
DECLARE @ItemID uniqueidentifier;
SET @ItemId = (select itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
DECLARE @CategoryID uniqueidentifier;
SET @CategoryID = '0FCA508F-7EB5-4C2E-8803-DE688C4126E5';
DECLARE @LineSequence int;
SELECT @LineSequence = ISNULL(MAX(LineSequence),0) + 1
FROM KenCatItemTest WHERE CategoryId = @CategoryId;
INSERT INTO ADVNET.dbo.KenCatItemTest
(CategoryItemId, ItemId, CategoryId, LineSequence)
VALUES (@CategoryItemId, @ItemId, @CategoryId, @LineSequence)
这个新语句抛出错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Msg 515, Level 16, State 2, Line 5 Cannot insert the value NULL into column 'ItemId', table 'ADVNET.dbo.KenCatItemTest'; column does not allow nulls. INSERT fails. The statement has been terminated.
此语句导致问题:
SET @ItemId = (select itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
如果满足条件的行不止一行,则会发生错误,因为 SET
语句期望在右侧返回标量值而不是行集。
要解决此问题,您可以将标准缩小到只返回一行的位置,或者执行类似提取第一个值的操作。
例如:
SET @ItemId = (select TOP 1 itemid from janel.dbo.item
WHERE janel.dbo.item.itemnumber like 'c-%' and listprice > 0 );
你不能只使用 Row_number() 来为 LineSequence 生成一个值吗?
INSERT INTO ADVNET.dbo.KenCatItemTest(
categoryitemid
,itemid
,categoryid
,LineSequence)
SELECT
NEWID()
,itemid
,'0FCA508F-7EB5-4C2E-8803-DE688C4126E5'
,ROW_NUMBER() OVER(ORDER BY ItemId)
FROM
janel.dbo.item i
WHERE
i.itemnumber LIKE 'c-%'
AND listprice > 0