Re: Insert into script
From: Keith Kratochvil (sqlguy.back2u_at_comcast.net)
Date: 11/03/04
- Next message: Melih SARICA: "is this a Bug in SQL Server 2000 ?"
- Previous message: Iain Duthie: "Re: Converting varchar value to a column of data type int."
- In reply to: Sonya: "Re: Insert into script"
- Next in thread: Sonya: "Re: Insert into script"
- Reply: Sonya: "Re: Insert into script"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 3 Nov 2004 06:29:27 -0600
> Are you referring to the fields that I am inserting, i.e.
entitiycategorydatabaseid?
No.
I am referring to the columns within the primary key.
CREATE TABLE #test (col1 int not null, col2 int not null, AnotherColumn
varchar(10))
ALTER TABLE #test ADD PRIMARY KEY NONCLUSTERED (col1, col2)
INSERT INTO #test (col1, col2, AnotherColumn) VALUES (1,2,'test')
INSERT INTO #test (col1, col2, AnotherColumn) VALUES (1,3,'test 2')
INSERT INTO #test (col1, col2, AnotherColumn) VALUES (2,4,'testing')
--this fails
INSERT INTO #test (col1, col2, AnotherColumn) VALUES (2,4,'testing')
--however, if you make the primary key unique the insert will work
INSERT INTO #test (col1, col2, AnotherColumn) VALUES (3,4,'testing')
SELECT * FROM #test
/*it appears that you are trying to do something like this
it fails because you are trying to insert duplicate data*/
INSERT INTO #test (col1, col2, AnotherColumn)
SELECT col1, col2, AnotherColumn FROM #test WHERE col2 <> 4
/*You will be able to insert the data if you make sure that the values
within the primary key are unique*/
INSERT INTO #test (col1, col2, AnotherColumn)
SELECT 9, col2, AnotherColumn FROM #test WHERE col2 <> 4
SELECT * FROM #test
-- Keith "Sonya" <Sonya@discussions.microsoft.com> wrote in message news:F6B89B7B-C526-4131-823F-28B16B0F646C@microsoft.com... > Keith, > > Thank you again for being so helpful. Can you explain what you mean by " > have to include each of the items within the primary key"? Are you referring > to the fields that I am inserting, i.e. entitiycategorydatabaseid?
- Next message: Melih SARICA: "is this a Bug in SQL Server 2000 ?"
- Previous message: Iain Duthie: "Re: Converting varchar value to a column of data type int."
- In reply to: Sonya: "Re: Insert into script"
- Next in thread: Sonya: "Re: Insert into script"
- Reply: Sonya: "Re: Insert into script"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|