Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 compare two tables without primary key

Author  Topic 

papa_sarr
Starting Member

6 Posts

Posted - 2011-08-10 : 18:24:31
I am trying to copy some records from Table A to Table B, and if successful delete them from Table A.
I use a temp table to save temporary the records to be deleted. My problem is that Tables A and B do not have a primary key. This is the way they were designed and I cannot alter them.
My question is that how can I compare the data between the temp table and Table A for the deletion knowing that there are no primary keys and each table has more than 20 columns.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-10 : 18:39:21
How are you determining what rows to copy? Put the insert/delete in a transaction and then you won't need to do a comparison.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

papa_sarr
Starting Member

6 Posts

Posted - 2011-08-10 : 20:28:42
Thank you for your response.
The rows to copy is based on the records present in the table at the time the copy is being done. Note that there are new records being added to the table A at any moment
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-10 : 20:33:04
You're going to need some way to identify the rows. It doesn't need to be a primary key, any unique set of columns will do. Does any set of columns make the row unique?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

papa_sarr
Starting Member

6 Posts

Posted - 2011-08-10 : 20:34:52
Hi Tara,

Between my insert and delete, new records are added into the table A. Therefore, I may delete records not yet inserted into Table B.

Thank you
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-10 : 20:39:34
Yeah that's why you need some way to identify them. So do any set of columns make the row unique?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-10 : 20:40:17
How about using an INSTEAD OF TRIGGER? You would just insert them into Table2 since you don't seem to want them in Table1.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

papa_sarr
Starting Member

6 Posts

Posted - 2011-08-10 : 21:09:49
There is no set of columns make the row unique, and this is the difficulty.
Go to Top of Page

papa_sarr
Starting Member

6 Posts

Posted - 2011-08-10 : 23:11:18
Table 1 is in the staging server.
Go to Top of Page
   

- Advertisement -