Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Cursor Help Expand / Collapse
Author
Message
Posted Friday, January 10, 2014 5:24 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 10:32 PM
Points: 197, Visits: 709
Thanks, this topic is Delete

Post #1530003
Posted Friday, January 10, 2014 6:02 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 11:16 AM
Points: 575, Visits: 3,750
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?
Post #1530005
Posted Friday, January 10, 2014 6:07 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 10:32 PM
Points: 197, Visits: 709
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);

Post #1530006
Posted Friday, January 10, 2014 6:28 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 11:16 AM
Points: 575, Visits: 3,750
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?
Post #1530011
Posted Friday, January 10, 2014 8:40 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 08, 2014 10:32 PM
Points: 197, Visits: 709
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?
Post #1530013
Posted Friday, January 10, 2014 10:03 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 11:16 AM
Points: 575, Visits: 3,750
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...
Post #1530015
Posted Friday, January 10, 2014 10:32 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 11:16 AM
Points: 575, Visits: 3,750
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.
Post #1530016
Posted Saturday, January 11, 2014 12:24 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:50 PM
Points: 22,504, Visits: 30,218
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.



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)
Post #1530053
Posted Saturday, January 11, 2014 1:40 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1530057
Posted Saturday, January 11, 2014 1:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1530058
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse