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. 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.
Try changing your INSERT statements from:
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 .
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. Most likely to do with your inner selects as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.