Use Cursor and If statement to store and replace data

562 Views Asked by At

I am transforming FoxPro code into SQL for a report. The current FoxPro Code is below. I need to be able to create two cursors, store data from two fields in them and check them against a table for replacing data. Can anyone help me transform this code into sql for sql server?

FoxPro Code

CREATE Cursor delete_cursor(salesno c(6), release_number c(3))     
SELECT table1    
GOTO TOP    
lcsono= table1.salesno
lcrelease = table1.release_number     
GOTO 2    
SCAN REST

   IF table1.salesno == lcsono AND table1.release_number <> "000"
      AND lcrelease = "000"

      SELECT delete_cursor
      APPEND BLANK
      REPLACE table1.salesno WITH lcsono
      REPLACE table1.release_number WITH lcrelease
      SELECT table1
   ENDIF

   lcsono = table1.salesno
   lcrelease= table1.release_number
ENDSCAN

SELECT delete_cursor
INDEX on ALLTRIM(delete_cursor.salesno) + ALLTRIM(delete_cursor.releaseno)
  TAG tagdelete

SELECT table1
GOTO TOP
SCAN
   IF SEEK(ALLTRIM(table1.salesno) + ALLTRIM (table1.salesno), 
      "delete_cursor", "tagdelete")

      REPLACE table1.delflag WITH "Y"
   ENDIF
ENDSCAN

My SQL Server Code

DECLARE CURSOR @fsono char(6)
update table1.salesno Set 
      table1.salesno = @fsono
   where 
          @fsono = table1.salesno 
      AND table1.release_number <> "000" 
      AND lcrelease = "000" 

DECLARE CURSOR @release char(6)

update table1.release_number Set 
      table1.release_number = @release
   where 
          @fsono = table1.salesno 
      AND table1.release_number <> "000" 
      AND @release = "000"
0

There are 0 best solutions below