Solved

Apply results from a stored procedure to multiple rows

Posted on 2009-04-07
2
194 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
  • 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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
encyps queries mssql 15 27
BULK INSERT most recent CSV 19 21
Test a query 23 18
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

759 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

24 Experts available now in Live!

Get 1:1 Help Now