sqlserver 遊標

遊標使用oracle

 

--新建ERP臨時表
if object_id('tempdb..#ERP_interface') is not null 
     drop table #ERP_interface
    
--將ERP中間表,寫入條碼系統臨時表
select * into #ERP_interface  from 
openquery(SYN_ERP, 'select * from FYG.FYG_INV_TRANS_INT_TEMP st ')
WHERE TS>'2016-01-01'

--select * from #ERP_interface

--新建條碼臨時表
if object_id('tempdb..#BarCode_interface') is not null 
     drop table #BarCode_interface

--將oracleinterface狀態爲1的數據,寫入條碼系統臨時表

select * into #BarCode_interface from [WMS_BarCode_V10_SH].[dbo].[OracleInterface] 
where status<>2 and operatetime>'2016-01-01' and status=1

--select * from #BarCode_interface

DECLARE @BarcodeId varchar(255)
DECLARE @TranceNo varchar(255)

DECLARE My_Cursor CURSOR                             --定義遊標
FOR (SELECT id FROM #BarCode_interface)                 --查出須要的集合放到遊標中
OPEN My_Cursor;                                      --打開遊標
FETCH NEXT FROM My_Cursor INTO @BarcodeId;           --讀取第一行數據
WHILE @@FETCH_STATUS = 0
    BEGIN

    UPDATE [WMS_BarCode_V10_SH].[dbo].[LabelTransactionInfo]
       SET [Status] = 1
     WHERE transactionid=@BarcodeId

        select @TranceNo=TraceNo from #BarCode_interface where id=@BarcodeId;
        
    UPDATE [WMS_BarCode_V10_SH].[dbo].[BizTransactionInfo]
       SET [Status] = 2      
     WHERE [WMS_BarCode_V10_SH].[dbo].[BizTransactionInfo].Traceno=@TranceNo
        
    if exists (select * from #ERP_interface where codebar_id=@BarcodeId and transaction_reference=@TranceNo)
            begin
                DECLARE @ERRORMESSGAE varchar(255)
                DECLARE @STATUS NVARCHAR(255)
                select @STATUS=[STATUS],@ERRORMESSGAE=ERROR_EXPLANATION from #ERP_interface where codebar_id=@BarcodeId and transaction_reference=@TranceNo
                IF(@STATUS IS NULL OR @STATUS='I')
                    PRINT 'IS DOING'
                ELSE
                    UPDATE [WMS_BarCode_V10_SH].[dbo].[OracleInterface] SET STATUS=-1,errormessage=@ERRORMESSGAE WHERE ID=@BarcodeId;
            end
        else
            BEGIN
                UPDATE [WMS_BarCode_V10_SH].[dbo].[OracleInterface] SET STATUS=2 WHERE ID=@BarcodeId;
            END       
            
        FETCH NEXT FROM My_Cursor INTO @BarcodeId;
    END
CLOSE My_Cursor;                                     --關閉遊標
DEALLOCATE My_Cursor;                                 --釋放遊標
相關文章
相關標籤/搜索