Solved

Apply results from a stored procedure to multiple rows

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

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!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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