?
Solved

Need to execute row by row process

Posted on 2011-02-15
7
Medium Priority
?
814 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
[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
  • 3
  • 3
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 143

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 143

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Backup & Restore 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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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