Solved

stored procedure that iterates through records and execute another process

Posted on 2015-02-20
11
59 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 45

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:ScottPletcher
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
 
LVL 69

Expert Comment

by:ScottPletcher
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
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 45

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now