Solved

stored procedure that iterates through records and execute another process

Posted on 2015-02-20
11
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 50

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
Stressed Out?

Watch some penguins on the livecam!

 
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 50

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

729 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