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
Solved

stored procedure that iterates through records and execute another process

Posted on 2015-02-20
11
71 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 48

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 48

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

791 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