Hi all,I have a table ProductType and another table ProductSubType. The ProductSubType table has a foreign key constraint with the ProductType table.I would like to insert a record "Common" in the ProductSubType table for each existing ProductType. I could use temporary tables or cursors, but I'm guessing there must be a better way of doing this. Any suggestions?The ProductType table
CREATE TABLE [dbo].[ProductType]( [ID] [int] IDENTITY(1,1) NOT NULL, [Description] [nvarchar](50) NOT NULL, CONSTRAINT [PK_ProductType] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
The ProductSubType table
CREATE TABLE [dbo].[ProductSubType]( [ID] [int] IDENTITY(1,1) NOT NULL, [FK_ProductType_ID] [int] NOT NULL, [Description] [nvarchar](50) NOT NULL, CONSTRAINT [PK_ProductSubType] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[ProductSubType] WITH CHECK ADD CONSTRAINT [FK_ProductSubType_ProductType] FOREIGN KEY([FK_ProductType_ID])REFERENCES [dbo].[ProductType] ([ID])GOALTER TABLE [dbo].[ProductSubType] CHECK CONSTRAINT [FK_ProductSubType_ProductType]GO
I was hoping I could do something like this (pseudo code)
For Each ID in ProductType If not exists(ID, 'Common') in ProductSubType Then insert into ProductSubType (FK_ProductType_ID,Description) values (ID,'Common') End IfNext
Thanks in advance!