SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Trying to erase duplicated rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rnavarro
Starting Member

Argentina
10 Posts

Posted - 09/16/2012 :  12:41:12  Show Profile  Reply with Quote
Hello people, i trying to erase duplicated rows with

DECLARE TEST_CURSOR CURSOR FOR
SELECT 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_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @Code
WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from tla_sg2org where "Code" = @Code
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @Code
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR

and i obtain this error

Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 2
Incorrect syntax near 'Code'.
Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 4
Incorrect syntax near 'CodeOrg'.
Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 8
Incorrect syntax near 'SGroup'.
Msg 102, Level 15, State 1, Server ARUSFSRV37, Line 15
Incorrect syntax near 'Code'.

with bcp utility but with SSMS no. Any help?

visakh16
Very Important crosS Applying yaK Herder

India
48064 Posts

Posted - 09/16/2012 :  12:48:46  Show Profile  Reply with Quote


DELETE a
FROM tla_sg2org a
INNER JOIN (SELECT CodeOrg,SGroup
FROM tla_sg2org 
GROUP BY CodeOrg, SGroup
HAVING COUNT (1) > 1) b
AND b.SGroup = a.SGroup
AND b.CodeOrg = a.CodeOrg


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rnavarro
Starting Member

Argentina
10 Posts

Posted - 09/17/2012 :  07:51:47  Show Profile  Reply with Quote
Thanks for your prompt response
your query give me this error

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'AND'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48064 Posts

Posted - 09/17/2012 :  11:13:06  Show Profile  Reply with Quote
that was a typo


DELETE a
FROM tla_sg2org a
INNER JOIN (SELECT CodeOrg,SGroup
FROM tla_sg2org 
GROUP BY CodeOrg, SGroup
HAVING COUNT (1) > 1) b
AND ON b.SGroup = a.SGroup
AND b.CodeOrg = a.CodeOrg




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000