Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor Help


Cursor Help

Author
Message
rocky_498
rocky_498
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 1291
Thanks, this topic is Delete
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2178 Visits: 12497
Cursor is a bad word around here... careful.

That said, I don't think you need a cursor at all.


CREATE Table #TableA
(
ID int IDENTITY(1,1),
SID INT,
FNAME varchar(50),
LNAME   varchar(50),
DOB    date,
CITY   varchar(50),
IsActive bit
);
GO

INSERT INTO #TableA
VALUES ('245','Smith','John','1/10/1998','Los Angles','0'); -- will be made Active=1.
INSERT INTO #TableA
VALUES ('298','Smith','John','1/10/1998','Los Angles','0');
INSERT INTO #TableA
VALUES ('987','Smith','John','1/10/1998','Los Angles','0');
INSERT INTO #TableA
VALUES ('451','Collins','Albert','3/1/2003','Los Angles','0'); -- will be made Active=1.

UPDATE #TableA
SET IsActive = 1
WHERE [SID] IN
   (SELECT MIN([SID]) AS GoodID
   FROM #TableA
   GROUP BY FName
      , LName
      , DOB
      , City);



results:
ID   SID   FNAME   LNAME   DOB   CITY   IsActive
1   245   Smith   John   1998-01-10   Los Angles   1 <-- first one of group, so set Active =1
2   298   Smith   John   1998-01-10   Los Angles   0
3   987   Smith   John   1998-01-10   Los Angles   0
4   451   Collins   Albert   2003-03-01   Los Angles   1 <-- first one of group, so set Active =1

right?
rocky_498
rocky_498
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 1291
Thank You for your reply,

That was a sample data that i post, User can select anything from front end to merge, it could be max sid or min sid don't know. I agree with you Cursor is very bad, sometime if you don't have no choice then. I will check later to optimize my SP. Could you please help me to find the problem, why cursor is running infinite loop?

Thank You.

UPDATE #TableA
SET IsActive = 1
WHERE [SID] IN
   (SELECT MIN([SID]) AS GoodID
   FROM #TableA
   GROUP BY FName
      , LName
      , DOB
      , City);
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2178 Visits: 12497
So in theory they could merge two records that don't match? Could you post an example? Explain why you can't do it with either a CTE or a simple update statement. What are the rules on how the stuff gets merged?
rocky_498
rocky_498
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 1291
Here is simple e.g

Here is my sample data
ID   SID   FNAME   LNAME   DOB   CITY   IsActive
2   245   Smith   John   1998-01-10   Los Angles   0
3   298   Smith   John   1998-01-10   Los Angles   0
4   987   Smith   John   1998-01-10   Los Angles   0

If I select in sp, SID = 245,Fname=Smith,Lname = John and DOB = 1998-01-10 is right, rest all the data in the data with Fname=Smith,Lname = John and DOB = 1998-01-10 to inactive, it could be 4/5/ or any numbers, so that's why i am using Cursor inside SP,

Exec Merge '245','smith','john','1998-01-10'

ID   SID   FNAME   LNAME   DOB   CITY   IsActive
2   245   Smith   John   1998-01-10   Los Angles   0
3   298   Smith   John   1998-01-10   Los Angles   1
4   987   Smith   John   1998-01-10   Los Angles   1

So end result should be like this from sample data,
make sense?
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2178 Visits: 12497
So sue me.

--Mark the records we want to delete, just to be sure we don't do anything terrible.

UPDATE #TableA
SET IsActive = 1
WHERE [SID] IN
   (SELECT MIN([SID]) AS GoodID
   FROM #TableA
   GROUP BY FName
      , LName
      , DOB
      , City);



-- now that the records are marked, delete those.

DELETE
FROM #TableA
WHERE IsActive = 0;



Race you... see which wins between that and using a cursor...
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2178 Visits: 12497
Okay, here's the DELETE as a CTE, which is probably the easiest way to do it... (once you read the crib notes)

; WITH CTE_DupeAddr(DupeID, ID, Cid, FName, LName, DOB, City) AS
(
   SELECT
      ROW_NUMBER() OVER (PARTITION BY FName, LName, DOB, City ORDER BY FName, LName) AS DupeID
      , ID
      , [SID]
      , FName
      , LName
      , DOB
      , City
   FROM #TableA
)
/*
-- show what will be deleted
SELECT * FROM CTE_DupeAddr
WHERE DupeID>1;
*/

DELETE
FROM CTE_DupeAddr
WHERE DupeID>1;



(Gotta love Rick Morelan's simple examples!)

comment out the SELECT/DELETE statement, depending on which you want to run. The Select will show which records will be deleted. The other will just delete them.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
rocky_498 (1/10/2014)
Thanks, this topic is Delete



May I ask why you deleted your original question? To be honest it is bad form especially since it is possible others my have the same question you had and now will have no idea what you had asked.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
rocky_498 (1/10/2014)
Thanks, this topic is Delete



Deleting your original question is a really good way to have people mentally black ball you in the future. ;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
pietlinden (1/10/2014)
So sue me.


Pretty much an uncalled for statement.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search