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


Replacing cursors...!!


Replacing cursors...!!

Author
Message
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
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
thomashohner
thomashohner
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 2197
I'm betting some may get irritated that you spamed this post 3 times. In my limited experince posting it once is enough.:-P

***SQL born on date Spring 2013:-)
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
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
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: 24233 Visits: 37978
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/

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)
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
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..
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: 24233 Visits: 37978
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.

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)
nandu.chowdary412
nandu.chowdary412
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 111
Thank you for your valuable explanation.!!...I'm trying to implement same concept and get back to you..!! once gain thank you..!!
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