Solved

Apply results from a stored procedure to multiple rows

Posted on 2009-04-07
2
197 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSAS Store Forecasting data in the cube 1 19
sql server query 18 41
SQL Availablity Groups List 2 8
How to structure query with count aggregate 4 18
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

832 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