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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Delete

Author  Topic 

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 05:10:09
I tried to delete records in table.

It gave me an error: Key column information is insufficient or incorrect. Too many rows were affected by update.

What does it mean?

- HELP -

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-09-24 : 05:20:51
where are you issuing the delete? can you post your statement?
Go to Top of Page

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 05:24:51
I am using SQL Server Enterprise Manager. I have a table called customer_list_for_prospect_prof$.

I get the rows using this query:
SELECT *
FROM customer_list_for_prospect_prof$
WHERE (pers_last_nm = 'Lim') AND (pers_give_nm = 'Roy')

It gives me 2 identical rows. I want to delect one away. I just right-click, and select 'Delete'. However, I was given the error mentioned above.

- HELP -
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-09-24 : 05:42:06
aha...a table with no primary key/index!!!
use a script in QA.....the less you use EM...the better...

There's an article here for resolving/removing duplicates
Go to Top of Page

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 05:46:18
Actually my table has primary key.

- HELP -
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-09-24 : 05:49:33
how about deleting the row using query analyzer?

delete from mytable where pk=key
Go to Top of Page

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 05:56:17
That will delete both rows right? I only want to delete one of them. The two rows are actually duplication.

- HELP -
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-09-24 : 05:57:59
but you said you have a primary key.

insert into #temp
select distinct <fields> from table1

delete from table1 where id=pk

insert into table1
select <fields> from #temp
Go to Top of Page

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 06:07:20
The table below is example:

pers_last_nm pers_give_nm cust_id
Lim Roy 1
Lim Roy 1 <- Duplication, I want to remove this away
Tan Jonathon 2

Jen, I dun quite understand what <fields> and #temp mean. Can you explain? Thx!



- HELP -
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-09-24 : 06:12:10
since i don't know your fields, i just enclosed them with < >,

what i mean is that you just enumerate your fields, The #temp will create a local temporary table for your session. so if you close your qa this will be dropped.

algorithm: select the data you want (use distinct so you'll get only 1 record),then drop the rows in your table, and copy the data you selected earlier on which are saved in #temp.
Go to Top of Page

doggi3
Yak Posting Veteran

62 Posts

Posted - 2004-09-24 : 06:48:56
ThX~

- HELP -
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-09-24 : 09:23:28
"Actually my table has primary key."

Then you can't have two identical rows!

If you have just one instance of a duplicate, and there are only two rows, you could do:

SET ROWCOUNT 1
delete from mytable where pk=key
SET ROWCOUNT 0 -- This is VERY important

This will restrict the delete to only deleting one row, so the other will be left behind

If you want to be "safe" then do this instead

BEGIN TRANSACTION
SELECT * from mytable where pk=key
SET ROWCOUNT 1
delete from mytable where pk=key
SET ROWCOUNT 0 -- This is VERY important
SELECT * from mytable where pk=key

Is you only see one row in the second resultset then issue the COMMIT command, otherwise issue a ROLLBACK command

Kristen
Go to Top of Page

Ketelaars
Starting Member

1 Post

Posted - 2004-10-14 : 09:01:09
Use a delete query in the Query-Analyser.
I had the same problem, dindn't solve it, but used the above
mentioned solution
Go to Top of Page
   

- Advertisement -