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.
Author |
Topic |
rnavarro
Starting Member
10 Posts |
Posted - 2012-09-16 : 12:41:12
|
Hello people, i trying to erase duplicated rows withDECLARE TEST_CURSOR CURSOR FORSELECT a."Code" FROM tla_sg2org a WHERE a."CodeOrg" IN (SELECT b."CodeOrg" FROM tla_sg2org b GROUP BY b."CodeOrg", b."SGroup" HAVING COUNT (b."SGroup") > 1) AND a."SGroup" IN (SELECT c."SGroup" FROM tla_sg2org c WHERE c."CodeOrg" = a."CodeOrg" GROUP BY c."CodeOrg", c."SGroup" HAVING COUNT (c."SGroup") > 1 ) AND a."Code" NOT IN ( SELECT MAX (d."Code") FROM tla_sg2org d WHERE d."CodeOrg" = a."CodeOrg" AND d."SGroup" = a."SGroup")ORDER BY "CodeOrg", "SGroup"DECLARE @Code Varchar(50)OPEN TEST_CURSORFETCH NEXT FROM TEST_CURSORINTO @CodeWHILE @@FETCH_STATUS=0BEGINBEGINdelete from tla_sg2org where "Code" = @CodeIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSORINTO @CodeENDCLOSE TEST_CURSORDEALLOCATE TEST_CURSORand i obtain this errorMsg 102, Level 15, State 1, Server ARUSFSRV37, Line 2Incorrect syntax near 'Code'.Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 4Incorrect syntax near 'CodeOrg'.Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 8Incorrect syntax near 'SGroup'.Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 15Incorrect syntax near 'Code'.with bcp utility but with SSMS no. Any help? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-16 : 12:48:46
|
DELETE aFROM tla_sg2org aINNER JOIN (SELECT CodeOrg,SGroupFROM tla_sg2org GROUP BY CodeOrg, SGroupHAVING COUNT (1) > 1) bAND b.SGroup = a.SGroupAND b.CodeOrg = a.CodeOrg ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
rnavarro
Starting Member
10 Posts |
Posted - 2012-09-17 : 07:51:47
|
Thanks for your prompt responseyour query give me this error Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AND'. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-17 : 11:13:06
|
that was a typoDELETE aFROM tla_sg2org aINNER JOIN (SELECT CodeOrg,SGroupFROM tla_sg2org GROUP BY CodeOrg, SGroupHAVING COUNT (1) > 1) bAND ON b.SGroup = a.SGroupAND b.CodeOrg = a.CodeOrg ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|