Hi,I need to insert a group of records into a table but some of the items might already exist. Due to primary key constraints it raises errors. I'm not sure how to proceed. Lets say a user has a list of products. They might already have some products but due to some event, they need all the products in that category. Since they might already have some items (and deleting them before inserting is not an option), I'm not sure how to add the remaining items. Please see the sample below. User 1 already has a 'dinner plate' but now I need to add ALL the 'houseware' items. How can I insert the 'coffee cup' and 'fork' without inserting the 'dinner plate'? The input parameters need to be the 'CustomerID' and 'Type'CREATE TABLE Products ( [ProductID] [int] NOT NULL , [Type] [varchar] (20) , [Name] [varchar] (100),)GOCREATE TABLE CustomerProducts ( [CustomerID] [int] NOT NULL , [ProductID] [int] NOT NULL ,)GOALTER TABLE CustomerProducts ADD CONSTRAINT [PK_CustomerProducts] PRIMARY KEY CLUSTERED ( [CustomerID], [ProductID] )GOINSERT INTO Products VALUES (1,'cars','ford')GOINSERT INTO Products VALUES (2,'cars','honda')GOINSERT INTO Products VALUES (3,'housewares','dinner plate')GOINSERT INTO Products VALUES (4,'housewares','coffee cup')GOINSERT INTO Products VALUES (5,'housewares','fork')GOINSERT INTO CustomerProducts VALUES (1,1)GOINSERT INTO CustomerProducts VALUES (1,3)GO-- this is where I need a sproc which will essentially-- insert records based on type and customerIDINSERT INTO CustomerProductsSELECT 1 ,ProductIDFROM ProductsWHERE Type = 'housewares'GODrop table CustomerProductsGODrop table ProductsGO
Nic