![](/img/trans.png)
[英]Subquery returned more than 1 value. This is not permitted when the subquery follows>= or when the subquery is used as an expression
[英]This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. More than 1 value was returned.
我嘗試此查詢,我得到以下錯誤。 我還收到一條錯誤消息,指出已違反主鍵約束PK_ITEMPATH。 我應該怎么做才能糾正此錯誤,因為查詢將無法成功運行
USE Sk_EN_UserDB
GO
SET NOEXEC OFF
GO
IF(DB_NAME() NOT LIKE '%_UserDB')
BEGIN
RAISERROR ('You must run this script in UserDB database',18,0)
SET NOEXEC ON
END
GO
IF(NOT EXISTS(SELECT * FROM UserRights.Path WHERE PathID = 'Myviews'))
BEGIN
insert into UserRights.Path(PathID, IsVendorSpecific)
values('Myviews', 0)
END
IF(NOT EXISTS(SELECT * FROM UserRights.Products WHERE ProjectName = 'Products'))
BEGIN
insert into UserRights.Products(ProductID, ProjectName)
values(
(select MAX(ProductID) + 1 from [UserRights].Products),
'Products'
)
END
insert into UserRights.ProductsToPath(ProductID, PathID)
values(
(select ProductID
from [UserRights].Products
where ProjectName = 'Products'),
'Myviews'
)
insert into UserRights.ModulesToProducts(ModuleID, ProductID)
values(
(select ModuleID
from [UserRights].[Modules]
where DisplayName = 'Products Product' or HierarchyName like '%Products Product%'),
(select ProductID
from [UserRights].Products
where ProjectName = 'Products')
)
首先,您的內部選擇可能每個INSERT語句返回多個值。
嘗試從以下位置更改INSERT語句:
insert into UserRights.ProductsToPath(ProductID, PathID)
values(
(select ProductID
from [UserRights].Products
where ProjectName = 'Products'),
'Myviews'
)
至:
insert into UserRights.ProductsToPath(ProductID, PathID)
(select TOP 1 ProductID
from [UserRights].Products
where ProjectName = 'Products'),
'Myviews'
如果您需要進行批量插入(每個語句多於1條記錄),請使用游標 。
關於ITEMPATH PK違規-您的問題沒有給我足夠有關表模式的信息,但是該錯誤表明您可能正在插入具有主鍵的記錄。 最有可能與您的內部選擇有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.