| Author |
Topic |
|
gopieee16
Starting Member
5 Posts |
Posted - 2011-06-15 : 06:29:53
|
| Hi all, I'm writing the following procedure to delete the duplicate records from employees table. Employees table contains following data :empid first_name last_name sal identitycolumn7 gopi kishan 656565 138 rachel andy 25463 149 fun green 5689 1510 zolskty kia 1656565 1611 james karen 56565 1712 santo sandy 6565 187 gopi kishan 656565 78 rachel andy 25463 89 fun green 5689 910 zolskty kia 1656565 1011 james karen 56565 1112 santo sandy 6565 12my logic is delete from employees where identitycolumn <> (select max(q) from employees bwhere b.first_name = employees.first_nameand my procedure is create procedure usp_testparse @p_column varchar(23)asbegin DELETE FROM employees WHERE identitycolumn <> (SELECT max(identitycolumn) FROM employees b WHERE employees.@p_column = b.@p_column)endif is run this i get the following error :Msg 102, Level 15, State 1, Procedure usp_testparse, Line 8Incorrect syntax near '@p_column'.Please help me in fix this, Thanks in advance.Regardsgopi |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-15 : 06:42:33
|
| That won't delete duplicates. It'll delete most of the table (if it worked).don't run this butDELETE FROM employees WHERE identitycolumn <> (SELECT max(identitycolumn) FROM employees b WHERE b.first_name = @p_column)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-15 : 06:45:25
|
| something more like this?delete tblfrom tbl tjoin (select firstname, lastname, id = MAX(id) from tbl group by firstname, lastname having COUNT(*) > 1) t2on t2.firstname = t.firstnameand t2.lastname = t.lastnameand t2.id <> t.id==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
gopieee16
Starting Member
5 Posts |
Posted - 2011-06-15 : 06:47:32
|
| The logic which i mentioned above is working and it will delete the duplicate rows (for example in the above table which i mentioned there are 12 rows with six duplicates and it will delete all the six rows thats wat i want)If i use "DELETE FROM employees WHERE identitycolumn <> (SELECT max(identitycolumn) FROM employees b WHERE b.first_name = @p_column)"it will delete only one row so i have to run this procedure/query nearly 6 times to delete the full duplicates records. |
 |
|
|
gopieee16
Starting Member
5 Posts |
Posted - 2011-06-15 : 06:52:29
|
| thanks for your logic to delete the duplicate rows right nw i want to know why i'm getting error when creating procedure please look into itthanks in advance |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-15 : 06:56:12
|
| Lookk at the delete statement I posted. You had tbl.@variable which is invalid. It's tbl.column or @variable.Note - that query won't do what you expect it t9o==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
gopieee16
Starting Member
5 Posts |
Posted - 2011-06-15 : 07:04:22
|
| I didnt get u can u explain "You had tbl.@variable which is invalid. It's tbl.column or @variable." |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-15 : 07:11:38
|
| b.@p_columnb is an alias for the table and this needs to reference a column in that tableI see what you are tryingb to do - maybeDELETE employees from employees eWHERE e.identitycolumn <> (SELECT max(e2.identitycolumn) FROM employees e2 WHERE e2.firstname = e.firstname)and e.firstname = @p_columnYou can drop the and e.firstname = @p_columnand it will do the whole table.You might want to nclude lastname as well thoughDELETE employees from employees eWHERE e.identitycolumn <> (SELECT max(e2.identitycolumn) FROM employees e2 WHERE e2.firstname = e.firstname and e2.lastname = e.lastname)I would go with my previous statement though as it only checks duplicates for the delete and gives a subquery that you can run to see what will be affected.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
gopieee16
Starting Member
5 Posts |
Posted - 2011-06-15 : 07:28:24
|
| Thanks for your clarification, I'll go with "DELETE employees from employees eWHERE e.identitycolumn <> (SELECT max(e2.identitycolumn) FROM employees e2 WHERE e2.firstname = e.firstname and e2.lastname = e.lastname)"Because i already created a procedure in Oracle with same logic which we are using now, and more over in a production database with more number of records it is very difficult to delete the duplicate records one by one right?The procedure which i created in oracle with 2nd logic is :create or replace procedure test_parse (p_table varchar2, pvalue varchar2)is den varchar2(100); v_count number(6) := length(regexp_replace(pvalue, '[[:alpha:]]', '')) - 1; query varchar2(20000);begin query:= 'DELETE FROM ' || p_table || chr(10) || ' WHERE ROWID <> ' || chr(10) || '( SELECT Max(ROWID) FROM ' || p_table || ' b ' || ' where b.'; for i in 1..v_count loop den := substr(pvalue, ((instr (pvalue, ',',1,i)+1)), (((instr (pvalue, ',',1,i+1))) - (((instr (pvalue, ',',1,i)+1)))) ); query := query || den || ' = ' || p_table || '.'|| den || chr(10) || ' and b.'; end loop; query := substr(query, 1, length(query) - 7) || ')'; EXECUTE IMMEDIATE query; end test_parse; |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-15 : 07:45:18
|
| You can run a similar query in oracle to that which I first posted.No need for a loop or dynamic sql.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|