Solved

Need to execute row by row process

Posted on 2011-02-15
7
789 Views
Last Modified: 2012-06-27
Hi there,
I need to write a cursor that allows me to execute a stored procedure for each row found on my first query.
I want to do something like this:
My first query returns a set of values based on X criteria
SELECT NUMBER_KEY
FROM CD_MAIN
WHERE DATA_STATUS = 'Generated Work Order to E.S.'
and ENTERED_DATE <=
      CONVERT(VARCHAR(10), DATEADD(day, -180, GETDATE()), 101)

a set of X NUMBER_KEY are returned and i need to execute the following stored procedure:
usp_DAL_UpdateViolationStatus passing the NUMBER_KEY value, 'some text', and todays date in short format. whats the best practice to do this?
thanks,
COHFL
 
0
Comment
Question by:COHFL
  • 3
  • 3
7 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34897658
you can do this:
declare @t table (NUMBER_KEY int)
DECLARE @number_key int
declare @short_date date

set @short_date = getdate()

insert into @t 
 SELECT NUMBER_KEY
   FROM CD_MAIN
  WHERE DATA_STATUS = 'Generated Work Order to E.S.'
   and ENTERED_DATE <= CONVERT(VARCHAR(10), DATEADD(day, -180, GETDATE()), 101)

WHILE @@ROWCOUNT> 0
BEGIN
  SELECT TOP 1 @NUMBER_KEY = NUMBER_KEY FROM @t

  if @@ROWCOUNT > 0
  BEGIN
    exec usp_DAL_UpdateViolationStatus @number_key, 'some text', @short_date 
  END

  DELETE @t WHERE NUMBER_KEY = @NUMBER_KEY
END

Open in new window

0
 
LVL 11

Expert Comment

by:rajvja
ID: 34897666
Store the first query result in a temp table. Temp table should have IDENTITY column.
Loop through temp table and call the stored procedure per each row.
0
 

Author Comment

by:COHFL
ID: 34897787
angelIII i get this error:
Msg 2715, Level 16, State 3, Line 2
Column or parameter #-2: Cannot find data type date.
Parameter '@short_date' has an invalid data type.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34897816
sorry.... datetime instead of date
0
 

Author Comment

by:COHFL
ID: 34897842
yeah i figure that one and another mistake but thats was my fault NUMBER_KEY is not a number so i changed to CHAR(10)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34897930
well, not mistake as such, as I could not guess the data type from NUMBER_KEY to be not numerical :)
so, does it work for you?
0
 

Author Comment

by:COHFL
ID: 34898004
yes it did thanks once again angelII.

"not mistake as such, as I could not guess" => but thats was my fault (it is ok it was my fault i did not point out the data type but like i said i changed and it worked fine)

you rock!!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to extract a "coded date" from a string field? 4 59
MS SQL 2005 Srink database in chunks 4 58
Delete from table 6 47
Anyway to make these 2 SQL statements into one? 13 39
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

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