[英]This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. More than 1 value was returned.
Iam trying this query and i get these following errors. 我尝试此查询,我得到以下错误。 I also get an error saying that the primary key constraint PK_ITEMPATH has been violated. 我还收到一条错误消息,指出已违反主键约束PK_ITEMPATH。 What should i do inorder to rectify this error as the query wont be able to run successfully 我应该怎么做才能纠正此错误,因为查询将无法成功运行
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')
)
First, your inner selects are probably returning more than 1 value per INSERT statement. 首先,您的内部选择可能每个INSERT语句返回多个值。
Try changing your INSERT statements from: 尝试从以下位置更改INSERT语句:
insert into UserRights.ProductsToPath(ProductID, PathID)
values(
(select ProductID
from [UserRights].Products
where ProjectName = 'Products'),
'Myviews'
)
to: 至:
insert into UserRights.ProductsToPath(ProductID, PathID)
(select TOP 1 ProductID
from [UserRights].Products
where ProjectName = 'Products'),
'Myviews'
If you need to do batch inserts (more than 1 record per statement), use a cursor . 如果您需要进行批量插入(每个语句多于1条记录),请使用游标 。
With regards to the ITEMPATH PK Violation - your question doesn't give me enough information about your table schema, but the error means that you're probably inserting a record with a primary key that already exists. 关于ITEMPATH PK违规-您的问题没有给我足够有关表模式的信息,但是该错误表明您可能正在插入具有主键的记录。 Most likely to do with your inner selects as well. 最有可能与您的内部选择有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.