?
Solved

Apply results from a stored procedure to multiple rows

Posted on 2009-04-07
2
Medium Priority
?
200 Views
Last Modified: 2012-05-06
Hi,

I have a table that I have generated using code similar as below and I would like to perform a stored procedure on all the rows in the table.  What is the best way to approach this?
SELECT col1, col2, col3 
INTO Details 
FROM dbo.CalibrationPoints;
 
ALTER TABLE Details ADD col4 REAL NULL;
 
SELECT @count = count(*) FROM Details;
 
WHILE @count  > 0
BEGIN
    -- EXEC spGetError @error OUTPUT, @calibrationID, referenceValue;
    -- UPDATE Details SET  col4 = @error  where col1 = @count;
    
    SELECT @count = @count -1;
END;
 
DROP TABLE Details;

Open in new window

0
Comment
Question by:quilkin
[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
  • 2
2 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24085803
Hi,

You could use a cursor.

/peter
DECLARE yourcursor CURSOR FOR 
SELECT col1, col2, col3 FROM dbo.CalibrationPoints
 
OPEN yourcursor
 
FETCH NEXT FROM yourcursor INTO @col1, @col2, @col3
 
WHILE @@FETCH_STATUS = 0 BEGIN
    --EXEC spGetError @error OUTPUT, @calibrationID, referenceValue
    --UPDATE Details SET col4 = @error  where col1 = @count
 
    FETCH NEXT FROM yourcursor INTO @col1, @col2, @col3
END 
 
CLOSE yourcursor
DEALLOCATE yourcursor

Open in new window

0
 
LVL 22

Accepted Solution

by:
pivar earned 1500 total points
ID: 24085827
Or with your code, picking up the value with select before using it with your sp. Declare @referenceValue and assign from proper column
SELECT col1, col2, col3 
INTO Details 
FROM dbo.CalibrationPoints;
 
ALTER TABLE Details ADD col4 REAL NULL;
 
SELECT @count = count(*) FROM Details;
 
WHILE @count  > 0
BEGIN
    select @referencValue=(?) from Details where col1 = @count
    -- EXEC spGetError @error OUTPUT, @calibrationID, @referenceValue;
    -- UPDATE Details SET  col4 = @error  where col1 = @count;
    
    SELECT @count = @count -1;
END;
 
DROP TABLE Details;

Open in new window

0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.
Suggested Courses

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