sql >> Database teknologi >  >> RDS >> Sqlserver

SQL-opdatering af en tabel baseret på værdier i markøren for en anden tabel

Du foretager to hentning før første opdatering. Prøv at omstrukturere din loop som:

DECLARE @so VARCHAR(50)
DECLARE @line VARCHAR(50)
DECLARE @pdate DATETIME
DECLARE @reason1 VARCHAR(50)
DECLARE @reason2 VARCHAR(50)
DECLARE @area VARCHAR(50)

DECLARE mycursor CURSOR LOCAL FAST_FORWARD FOR 
SELECT [bp_so_number],[bp_line],[bp_production_date],[bp_reason1],[bp_reason2],[bp_area]
 FROM [Workflow].[dbo].[v_OTD_PostSrc]

 OPEN mycursor
 -- Always true
 WHILE 1 = 1
 BEGIN
    -- Get next record from cursor
    FETCH NEXT FROM mycursor
               INTO @so, @line, @pdate, @reason1, @reason2, @area;
    -- If there was a record @@fetch_status will be 0; 
    -- if not, or in case of an error, break the loop
    IF @@FETCH_STATUS <> 0
       break
    UPDATE otd_data SET [email protected], [email protected], [email protected] 
    WHERE [Order Number][email protected] AND [email protected] AND [Schedule Date][email protected] AND [Qty Open]>0
    --SELECT * FROM otd_data WHERE [Order Number][email protected] AND [email protected] AND [Schedule Date][email protected]
 END

CLOSE mycursor
DEALLOCATE mycursor

Jeg har tilføjet LOCAL and FAST_FORWARD a> muligheder til markøren. Dokumentation på FAST_FORWARD:




  1. Hvordan vi kan bruge mysql_affected_rows() i lagret procedure

  2. Mysql-forespørgsel:match mod brug af jokertegn

  3. Bruger env variabel i Spring Boots application.properties

  4. Oracle Invoke REST Service fra pl/sql