SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor Help


Cursor Help

Author
Message
rocky_498
rocky_498
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1150 Visits: 1417
Thanks, this topic is Delete
pietlinden
pietlinden
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14157 Visits: 14135
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
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1150 Visits: 1417
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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14157 Visits: 14135
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
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1150 Visits: 1417
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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14157 Visits: 14135
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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14157 Visits: 14135
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 Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95437 Visits: 38968
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 Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216272 Visits: 41986
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216272 Visits: 41986
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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