December 3, 2008 at 10:47 am
i need to insert into a table that has this scenario:
insert into table1 (col1, col2, col3, col4)
select (1,2,3,4) from table1 A join table2 B on
A.col1 = B.col2
**this works , but the second insert doesnt work
insert into table1 (col1,col2,col3,col4)
select (1,5,3,4) from table1 A join table2 B on
A.col1 = B.col2
where B.col2 = '5' and B.col2 is null
**this is not working
December 3, 2008 at 10:54 am
where B.col2 = '5' and B.col2 is null
-------------------------------------------
B.col2 can't be both '5' and NULL at the same time.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 3, 2008 at 10:58 am
when i try and run it with only
where B.col2 = '5'
the error is : Violation of PRIMARY KEY constraint
its prob looking at the column its already inserted with the same values?
December 3, 2008 at 11:07 am
Yes. Primary Key constraints force columns to be unique. For example, you usually have an employee ID number as a primary key for an employees table to make sure you don't accidentally give two of your employees the same id and confuse their information.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 3, 2008 at 11:08 am
Please refer to Books On Line (BOL) ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/edc46c29-51e0-4407-94a3-e68fd658ec3c.htm which states:
A table can have only one PRIMARY KEY constraint, and a column that participates in the PRIMARY KEY constraint cannot accept null values. Because PRIMARY KEY constraints guarantee unique data, they are frequently defined on an identity column.
When you specify a PRIMARY KEY constraint for a table, the SQL Server 2005 Database Engine enforces data uniqueness by creating a unique index for the primary key columns.
It would also be to your benefit to read the article in my signature block and prepare your question as the article describes so those here can give you adequate help.
December 3, 2008 at 2:06 pm
Is this the same primary key violation you've already posted like 3 other times that we keep telling you you can't do?
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply