We help IT Professionals succeed at work.
Get Started

SQL Server 2014 Cursor in stored procedure never seems to move past the first record. I.e it does not loop

109 Views
Last Modified: 2015-12-14
Examine the following piece of code:
        set @RM = 'some value'
        set @BASEQTY = 1
        DECLARE UOMSCHED CURSOR SCROLL FOR
      SELECT UOFM,EQUOMQTY FROM tablename WHERE column = @RM
      OPEN UOMSCHED
      FETCH first FROM UOMSCHED INTO @UOFM,@EQUOMQTY
        WHILE  @@FETCH_STATUS = 0
        BEGIN
                  set @cursor_Rows = @@Cursor_rows
                  SET @RMQTYCONVERTED = @BASEQTY /@EQUOMQTY
                  INSERT INTO RM_QTYS (RM_ID,[rmUOM],[QTYrequired])
                  VALUES (@RM,@UOFM,@RMQTYCONVERTED)
            END
      FETCH next FROM UOMSCHED INTO @UOFM,@EQUOMQTY
      CLOSE UOMSCHED;
      DEALLOCATE UOMSCHED;

In this example the SQL Query returns 5 rows. The @@cursor_rows variable also shows that the cursor has 5 rows.
However the variables @UOFM and @RMQTYCONVERTED always store the values of the first record. In other words the cursor does not seem to move to the next record. The purpose of this loop is to determine the quantity of something in 5 different units of measure. The units of measure and the conversion factors are stored in 'tablename'. All 5 calculated results are then inserted into another table 'RM_QTYS'. If there is a way to perform this without a cursor I am happy as well.
Comment
Watch Question
Top Expert 2013
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE