SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert Into Where NOT Exists


Insert Into Where NOT Exists

Author
Message
shannonphillips
shannonphillips
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 17
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)SSC Guru (191K reputation)

Group: General Forum Members
Points: 191478 Visits: 11908
shannonphillips - Thursday, December 28, 2017 1:32 PM
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Will something like this work for you?

WITH cteNew(Name, Serial) AS (
SELECT ComputerName, [Service Tag]
FROM dbo.ImpTbl_ComputerInventory
EXCEPT
SELECT ComputerName, SerialNumber
FROM dbo.ComputerInventory
)
INSERT INTO dbo.ComputerInventory(ComputerName, SerialNumber)
SELECT Name, Serial
FROM cteNew;



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)

Group: General Forum Members
Points: 274001 Visits: 41203
shannonphillips - Thursday, December 28, 2017 1:32 PM
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Try this:

INSERT INTO
[Tbl_ComputerInventory]
(
[ComputerName]
, [SerialNumber]
)
SELECT DISTINCT
[ImpTbl_ComputerInventory].[Computer Name]
, [ImpTbl_ComputerInventory].[Service Tag]
FROM
[ImpTbl_ComputerInventory];


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
shannonphillips
shannonphillips
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 17
Lynn Pettis - Thursday, December 28, 2017 1:59 PM
shannonphillips - Thursday, December 28, 2017 1:32 PM
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Try this:

INSERT INTO
[Tbl_ComputerInventory]
(
[ComputerName]
, [SerialNumber]
)
SELECT DISTINCT
[ImpTbl_ComputerInventory].[Computer Name]
, [ImpTbl_ComputerInventory].[Service Tag]
FROM
[ImpTbl_ComputerInventory];


Still getting duplicate error
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)

Group: General Forum Members
Points: 274001 Visits: 41203
shannonphillips - Thursday, December 28, 2017 2:27 PM
Lynn Pettis - Thursday, December 28, 2017 1:59 PM
shannonphillips - Thursday, December 28, 2017 1:32 PM
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Try this:

INSERT INTO
[Tbl_ComputerInventory]
(
[ComputerName]
, [SerialNumber]
)
SELECT DISTINCT
[ImpTbl_ComputerInventory].[Computer Name]
, [ImpTbl_ComputerInventory].[Service Tag]
FROM
[ImpTbl_ComputerInventory];


Still getting duplicate error


Now add the WHERE clause:

INSERT INTO
[Tbl_ComputerInventory]
(
[ComputerName]
, [SerialNumber]
)
SELECT DISTINCT
[ci2].[Computer Name]
, [ci2].[Service Tag]
FROM
[ImpTbl_ComputerInventory] [ci2]
WHERE
NOT EXISTS (SELECT 1
FROM [Tbl_ComputerInventory] [ci1]
WHERE [ci2].[Computer Name] = [ci1].[Computer Name] AND [ci2].[Service Tag] = [ci1].[Service Tag]);


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
shannonphillips
shannonphillips
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 17
Ed Wagner - Thursday, December 28, 2017 1:58 PM
shannonphillips - Thursday, December 28, 2017 1:32 PM
Background, I have imported dirty data with duplicate rows and bad field names into a Table called ImpTbl_ComputerInventory.
To clean the data I've created Tbl_ComputerInventory with:
CONSTRAINT [Pkey_Tbl_ComputerInventory] PRIMARY KEY CLUSTERED
(
[ComputerName] ASC,
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]?
Now I want to copy the non duplicate rows into Tbl_ComputerInventory using a Query such as:

INSERT INTO Tbl_ComputerInventory (ComputerName, SerialNumber)
SELECT ImpTbl_ComputerInventory.[Computer Name], ImpTbl_ComputerInventory.[Service Tag]
FROM ImpTbl_ComputerInventory
Where NOT EXISTS (Select ComputerName, SerialNumber From Tbl_ComputerInventory WHERE (Tbl_ComputerInventory.ComputerName = ImpTbl_ComputerInventory.[Computer Name] AND Tbl_ComputerInventory.SerialNumber = ImpTbl_ComputerInventory.[Service Tag]));

However I'm still getting: Violation of PRIMARY KEY constraint 'Pkey_Tbl_ComputerInventory'. Cannot insert duplicate key in object 'dbo.Tbl_ComputerInventory

Thanks in advance :-)

Will something like this work for you?

WITH cteNew(Name, Serial) AS (
SELECT ComputerName, [Service Tag]
FROM dbo.ImpTbl_ComputerInventory
EXCEPT
SELECT ComputerName, SerialNumber
FROM dbo.ComputerInventory
)
INSERT INTO dbo.ComputerInventory(ComputerName, SerialNumber)
SELECT Name, Serial
FROM cteNew;


gives me duplicate errors as well.
shannonphillips
shannonphillips
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 17


Not following your references to the Red 1 char or ci1, ci2 ?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)

Group: General Forum Members
Points: 274001 Visits: 41203
shannonphillips - Thursday, December 28, 2017 2:46 PM


Not following your references to the Red 1 char or ci1, ci2 ?


?????

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)SSC Guru (274K reputation)

Group: General Forum Members
Points: 274001 Visits: 41203
The "red" 1 is just a 1. ci1 and ci2 are table aliases.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)SSC Guru (125K reputation)

Group: General Forum Members
Points: 125094 Visits: 15067
Can you post the table definitions including collation for character columns?

If your import table has a case-sensitive collation and your destination table does not you could still get duplicates with all the solutions provided already.

Fixed-length vs. variable length could potentially be an issue here as well.



Jack Corbett
Consultant Straight Path Solutions
Dont let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a QuestionHow to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search