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

Replacing cursors...!! Expand / Collapse
Author
Message
Posted Sunday, June 8, 2014 11:48 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, August 23, 2014 12:36 AM
Points: 25, Visits: 90
Hi everyone, I'm new to Cursors, I just have idea how they work..recently i'm working on cursors and planning to re-write code...but i'm finding difficulty . Please give your suggestions and ideas much appreciated.thanks in advance.

DECLARE @ID varchar(12)
DECLARE @Volume int, @Paid int, @Denied int
DECLARE @PaidAmount money, @DeniedAmount money
DECLARE @CID varchar(12), @CStatus varchar(2), @amt_paid money

SET @Volume = 0
SET @Paid = 0
SET @Denied= 0
SET @PaidAmount = 0.00
SET @DeniedAmount = 0.00

DECLARE MainTableCursor cursor FOR
SELECT distinct root_Num FROM T1

OPEN MainTableCursor
IF @@cursor_rows = 0
GOTO CloseCursor

FETCH next from MainTableCursor into @ID
WHILE @@fetch_status = 0

BEGIN

DECLARE SubtableCursor cursor
FOR
SELECT CID,CStatus, Amt_paid FROM T1
WHERE root_num=@ID AND CStatus= '02'

OPEN SubtableCursor

FETCH next from SubtableCursor into @CID,@CStatus,@amt_paid
WHILE @@fetch_status =0

BEGIN
SET @volume = @volume + 1
if @CStatus='02' and @amt_paid > 0
BEGIN
SET @Paid = @Paid + 1
SET @PaidAmount = @PaidAmount + @amt_paid
END

if @CStatus='02' and @amt_paid <= 0
BEGIN
SET @Denied = @Denied + 1
SET @DeniedAmount = @DeniedAmount + @amt_paid
END

FETCH next from SubtableCursor into @CID,@CStatus,@amt_paid

END
CLOSE SubtableCursor

DEALLOCATE SubtableCursor

INSERT INTO Table2
SELECT @ID, @volume as V, @Paid,@Denied, @PaidAmount,@DeniedAmount

SET @volume = 0
SET @Paid = 0
SET @Denied= 0
SET @PaidAmount = 0.00
SET @DeniedAmount = 0.00

FETCH next from MainTableCursor into @ID

END

CLOSE MainTableCursor

CloseCursor:
DEALLOCATE MainTableCursor
Post #1578670
Posted Sunday, June 8, 2014 11:59 AM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Yesterday @ 7:15 PM
Points: 179, Visits: 1,038
I'm betting some may get irritated that you spamed this post 3 times. In my limited experince posting it once is enough.



***SQL born on date Spring 2013
Post #1578674
Posted Sunday, June 8, 2014 12:05 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, August 23, 2014 12:36 AM
Points: 25, Visits: 90
Yeah.Sry.I'm new, don't know where exactly need to post..!! I never used cursors as well :..can you please guide me how to delete..thanks
Post #1578675
Posted Sunday, June 8, 2014 12:13 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 7:28 AM
Points: 20,676, Visits: 32,267
Looking at the code you posted, I have no idea where to start. There actually isn't much to work with.

You can start by reading these articles:

http://www.sqlservercentral.com/articles/T-SQL/66097/
http://www.sqlservercentral.com/articles/T-SQL/66494/



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 #1578678
Posted Sunday, June 8, 2014 12:17 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, August 23, 2014 12:36 AM
Points: 25, Visits: 90
Thanks for your reply and suggestions.!! 25million rows in source table and its taking more than 5hrs to run..is there any way to load all data into temp table? and do changes..finally loading into 'Final' Table..
Post #1578679
Posted Sunday, June 8, 2014 12:55 PM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 7:28 AM
Points: 20,676, Visits: 32,267
First, I reformatted your code:


DECLARE @ID varchar(12)
DECLARE @Volume int, @Paid int, @Denied int
DECLARE @PaidAmount money, @DeniedAmount money
DECLARE @CID varchar(12), @CStatus varchar(2), @amt_paid money

SET @Volume = 0
SET @Paid = 0
SET @Denied= 0
SET @PaidAmount = 0.00
SET @DeniedAmount = 0.00

DECLARE MainTableCursor cursor FOR
SELECT distinct root_Num FROM T1

OPEN MainTableCursor
IF @@cursor_rows = 0
GOTO CloseCursor

FETCH next from MainTableCursor into @ID
WHILE @@fetch_status = 0
BEGIN
DECLARE SubtableCursor cursor
FOR
SELECT CID, CStatus, Amt_paid FROM T1
WHERE root_num = @ID AND CStatus = '02'

OPEN SubtableCursor

FETCH next from SubtableCursor into @CID, @CStatus, @amt_paid
WHILE @@fetch_status =0
BEGIN
SET @volume = @volume + 1
if @CStatus ='02' and @amt_paid > 0
BEGIN
SET @Paid = @Paid + 1
SET @PaidAmount = @PaidAmount + @amt_paid
END

if @CStatus ='02' and @amt_paid <= 0
BEGIN
SET @Denied = @Denied + 1
SET @DeniedAmount = @DeniedAmount + @amt_paid
END

FETCH next from SubtableCursor into @CID, @CStatus, @amt_paid
END
CLOSE SubtableCursor
DEALLOCATE SubtableCursor

INSERT INTO Table2
SELECT @ID, @volume as V, @Paid,@Denied, @PaidAmount,@DeniedAmount

SET @volume = 0
SET @Paid = 0
SET @Denied= 0
SET @PaidAmount = 0.00
SET @DeniedAmount = 0.00

FETCH next from MainTableCursor into @ID

END
CLOSE MainTableCursor
CloseCursor:
DEALLOCATE MainTableCursor


This made it easier to read. You really need to take the time to format for readability.

Second, with no information regarding the tables involved and using only the code you posted I was able to come up with the following:


INSERT INTO Table2
select
root_Num,
count(root_Num) as Volume,
sum(case when Amt_paid > 0 then 1 else 0 end) as Paid,
sum(case when Amt_paid <= 0 then 1 else 0 end) as Denied,
sum(case when Amt_paid > 0 then Amt_paid else 0 end) as PaidAmt,
sum(case when Amt_paid <= 0 then Amt_paid else 0 end) as DeniedAmt
from
T1
where
CStatus = '02'
group by
root_Num;



I will not guarantee that this code works since I have nothing to test against. You should also take the time to read the first article I reference below in my signature block regarding what you should post and how to post it for the best possible answers and get tested code in return.



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 #1578681
Posted Sunday, June 8, 2014 10:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, August 23, 2014 12:36 AM
Points: 25, Visits: 90
Thank you for your valuable explanation.!!...I'm trying to implement same concept and get back to you..!! once gain thank you..!!
Post #1578697
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse