How to use Cursor to Execute stored procedure where featurevalue is bigger than 0 ?

ahmed salah 3,216 Reputation points
2020-12-19T23:58:46.41+00:00

I work on SQL server 2012 . I face issue I can't use cursor to execute stored procedure when feature value is bigger than 0 .

I have stored procedure must execute with every line where feature value is bigger than 0

so according to my case I will use cursor to execute stored procedure with feature value that bigger than 0

so EXEC sp_updatevaluesonTables 10,30,40,50

this sp pass to it feature value to do some thing update and insert data

so How to use cursor to execute stored procedure sp_updatevaluesonTables with every value bigger than 0
as
cursor
EXEC sp_updatevaluesonTables 10,30,40,50
end cursor

im not use cursor before so can you help me

create table #updatestatus
(
FeatureValue int,
status nvarchar(50)
)
insert into #updatestatus
values 
(10,NULL),
(50,NULL),
(-2,NULL),
(-3,NULL),
(30,NULL),
(40,NULL)

SELECT * FROM #updatestatus WHERE FeatureValue>0

Exec sp_updatevaluesonTables FEATUREVALUE

Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2020-12-20T10:51:12.767+00:00

    I will have to assume that your procedure takes a single parameter, although you list it as it is taking four parameters.

    By the way, don't call your procedures sp_something. The sp_ prefix is reserved for system procedure, and would Microsoft ship a system procedure sp_updatevalue in a future release of SQL Server, this is the procedure that would be invoked, not your.

    Here is a cursor:

    DECLARE @cur CURSOR,  
            @value int  
      
    SET @cur = CURSOR STATIC FOR  
       SELECT FeatureValue FROM #updatestatus WHERE FeatureValue > 0  
      
    OPEN @cur  
      
    WHILE 1 = 1  
    BEGIN  
       FETCH @cur INTO @value  
       IF @@fetch_status <> 0 BREAK  
          
       EXEC sp_updatevalue @value  
    END  
    

    You declare a cursor variable, and then you assign that cursor with the SET statement. Always use the syntax that I showed you: CURSOR STATIC FOR followed by the SELECT statement to select the data.

    The OPEN command is the statement that actually populates the cursor.

    You loop over the cursor until you have gotten all rows. The FETCH statement retrieves one row at a time, and the variables in the FETCH statement must match the columns in the SELECT statement that populates the cursor. @@Fetch _status is a function which returns a non-zero value when there are no more rows in the cursor.

    If you google around, you will find different ways to run cursors, using a different syntax, and also a different disposition of the WHILE loop. However, I submit that the above is the optimal way to run a cursor.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jeffrey Williams 1,896 Reputation points
    2020-12-20T16:10:42.65+00:00

    Don't forget to close and deallocate the cursor...


  2. EchoLiu-MSFT 14,621 Reputation points
    2020-12-25T07:36:31.833+00:00

    Do you have any updates?

    Echo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.