简体   繁体   English

当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。 返回了多个值。

[英]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.

相关问题 子查询返回的值超过1。 当子查询遵循&gt; =或将子查询用作表达式时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows>= or when the subquery is used as an expression 子查询返回超过 1 个值。 当子查询跟随......或当子查询用作表达式时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows … or when the subquery is used as an expression 如何解决错误:“子查询返回了多个值”。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或用作表达式时,不允许使用 - How to fix error: “Subquery returned more than 1 value”. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或用作表达式时,不允许使用 - Subquery returned more than 1 value. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression SQL Server子查询返回了多个值。 当子查询跟随(字符)或子查询用作表达式时,不允许这样做 - SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows (chars) or when the subquery is used as an expression 子查询返回的值超过1。 当子查询遵循=,!= 、、&gt; =或将子查询用作expr时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, , >= or when the subquery is used as an expr 当子查询跟随子查询返回大于1的值时,这是不允许的 - This is not permitted when the subquery follows Subquery returned more than 1 value 子查询返回的值超过1。 当子查询遵循=,!=时,不允许这样做 - Subquery returned more than 1 value. This is not permitted when the subquery follows =, != 子查询返回的值超过1。 当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =…?时,不允许这样做。 - Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=, <, <= , >, >= …? 子查询返回的值超过1。 当子查询遵循=时,这是不允许的 - Subquery returned more than 1 value. This is not permitted when the subquery follows =
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM