Solved

stored procedure that iterates through records and execute another process

Posted on 2015-02-20
11
66 Views
Last Modified: 2015-04-30
Hi there,
Currently i have a select statement that returns account information and devices. something like this:
SELECT * FROM ACCOUNTS WHERE STATUS='ACTIVE'

Open in new window

Ok, fine that returns all of my active accounts. this result set gives me Device, PortNumber, Account, Units, XrefID now i need to call a second process that pass the Device and PortNumber to get me the final result. so the call looks like this:
EXEC CalcConsumption [DEVICEFROMPREVIOUSQUERY], [PORTNUMBERFROMPREVIOUSQUERY, '12-22-2014','02-15-2015'

Open in new window

whats the best practice to do something like this and how do i go about it?
Thanks!
0
Comment
Question by:COHFL
  • 5
  • 4
  • 2
11 Comments
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40621415
You can use a cursor:
DECLARE @Device VARCHAR(50)
DECLARE @PortNumber INT

DECLARE CursorName CURSOR FOR  
SELECT DEVICE, PORTNUMBER
FROM ACCOUNTS 
WHERE STATUS='ACTIVE'

OPEN CursorName 
FETCH NEXT FROM CursorName INTO @Device, @PortNumber 

WHILE @@FETCH_STATUS = 0   
BEGIN   
       EXEC CalcConsumption  @Device, @PortNumber, '12-22-2014','02-15-2015'

       FETCH NEXT FROM CursorName INTO @Device, @PortNumber
END   

CLOSE CursorName   
DEALLOCATE CursorName

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40621428
If it's not a huge number of rows, say 20K or less,  you can use a cursor.  For counts above that, I'd suggest generating dynamic SQL with a string of EXECs in it.

Also, be sure to explicitly specify FAST_FORWARD and LOCAL on the cursor, for efficiency:

DECLARE cursor_devices FAST_FORWARD LOCAL CURSOR
FOR
...
0
 

Author Comment

by:COHFL
ID: 40621440
the first return set is about 40k records
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40621458
Hmm, still borderline (20K is a very rough estimate :-) ); however, if you leave the cursor dynamic and updateable (i.e. don't specify fast_forward), then I would limit it to 20K.  Try the cursor and see how it performs.  If you think it's too slow, we can try another approach.
0
 

Author Comment

by:COHFL
ID: 40621481
Msg 153, Level 15, State 1, Procedure usp_..., Line 34
Invalid usage of the option local in the DECLARE CURSOR statement.
Msg 153, Level 15, State 1, Procedure usp_..., Line 34
Invalid usage of the option fast_forward in the DECLARE CURSOR statement.

i get this after i add
DECLARE Cons FAST_FORWARD LOCAL CURSOR FOR
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40621487
Oops, sorry, got the syntax wrong, CURSOR has to go first for those options:

DECLARE Cons CURSOR FAST_FORWARD LOCAL FOR
0
 

Author Comment

by:COHFL
ID: 40621496
Got it let me see if this gives me what i need and how long it will take
0
 

Author Comment

by:COHFL
ID: 40621526
Im getting this error: An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.
error.JPG
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40621535
Odd.  Hard to believe you're so short of RAM that a single cursor causes that.  Make sure you DEALLOCATE any previous cursor(s).
0
 

Author Comment

by:COHFL
ID: 40621541
I dont have any other cursor running.
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40625236
You may working with a large amount of data so I'll recommend you to declare the cursor with the following options:
DECLARE Cons CURSOR FORWARD_ONLY READ_ONLY FOR
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question