If you have a table with two primary keys:
CREATE TABLE [AttributeMap](
[intParentAttributeId] [bigint] NOT NULL,
[intChildAttributeId] [bigint] NOT NULL,
[datCreatedDate] [datetime] NOT NULL
CONSTRAINT [PK_AttributeMap] PRIMARY KEY CLUSTERED
(
[intParentAttributeId] ASC,
[intChildAttributeId] ASC
)
If you want to do an insert into/select statement to add data to the table, how can you restrict the data to make sure it doesn’t violate both keys?
So if you insert this into the table above:
INSERT INTO [AttributeMap] VALUES (1, 1, getdate())
INSERT INTO [AttributeMap] VALUES (1, 2, getdate())
INSERT INTO [AttributeMap] VALUES (1, 3, getdate())
INSERT INTO [AttributeMap] VALUES (2, 1, getdate())
How could you run this query without violating the keys?
declare table @temp (intParent int, intChild int)
insert into @temp (1, 1)
insert into @temp (1, 2)
insert into @temp (4, 4)
insert into @temp (5, 5)
insert into AttributeMap (intParentAttributeId, intChildAttributeId, datCreatedDate)
select intParent, intChild, getDate()
from @temp
So AttributeMap should end up with two new rows, values 4, 4, “date” and 5, 5 “date”. Make sense?
Cheers,
Matt
EXCEPTTry this: