SQL Server存储过程不更新表

时间:2018-06-06 14:56:06

标签: sql-server sql-server-2016

目前我有一个存储过程a.k.a主要入口程序,其处理如下:

CREATE PROCEDURE [JDSports].[SYC_LoopTemp_0All]
AS
BEGIN
    DECLARE @rundate as datetime
    SET @rundate = getdate()

    INSERT INTO RecordTime (record_date) 
        (SELECT @rundate)

    EXEC  SYC_LoopTemp_1New;

    UPDATE recordtime 
    SET _1New = 1 
    WHERE record_date = @rundate;

    EXEC SYC_LoopTemp_2Normal;

    UPDATE recordtime 
    SET _2Normal = 1 
    WHERE record_date = @rundate;

    EXEC SYC_LoopTemp_3Reprint;

    UPDATE recordtime 
    SET _3Reprint = 1 
    WHERE record_date = @rundate;

    EXEC SYC_LoopTemp_4Error;

    UPDATE recordtime 
    SET _4Error = 1 
    WHERE record_date = @rundate;
END

我目前面临的问题是,在成功运行这些过程后,record_date表现在会显示列_1New_4Error的值(这四列是smallint数据类型)如果我将update命令作为单个单独的SQL语句运行,则可以更新它。有趣的是,在事件发生前几天,记录中的值正确显示了四列中的值1.

我检查了其他四个存储过程并且运行正常,这些错误在运行时都没有显示。

程序或目标表的任何部分是否有问题,或者这可能是由于BCP流程还没有完全完成?

提前致谢。

更新 是的,程序通过BCP运行。如果我想运行4个内部程序中的一个,我也可以运行SSMS。

SYC_LoopTemp_1New:将新客户,收银员,物品和销售等插入各自的表格中。现有的不会被添加,因为它是多余的。

 INSERT INTO customer (name) (
        SELECT DISTINCT customer_desc FROM temp_receipt WHERE customer_desc NOT IN 
        (select name from customer)
        );

        INSERT INTO salesPerson (name) (
            SELECT DISTINCT salesPerson_desc FROM temp_purchase WHERE salesPerson_desc NOT IN 
            (select name from salesPerson)
        );

        INSERT INTO discRef ([desc]) (
            SELECT DISTINCT discRef FROM temp_purchase WHERE discRef NOT IN 
            (select [desc] from discRef)
        );

        INSERT INTO outlet (id, [desc]) (
        SELECT DISTINCT outlet_desc, outlet_desc FROM temp_receipt WHERE outlet_desc NOT IN 
        (select id from outlet)
        );

        INSERT INTO pos ([desc], outlet_id) (
        SELECT DISTINCT pos_desc, outlet_desc FROM temp_receipt WHERE temp_receipt.pos_desc NOT IN 
        (select [desc] from pos)
        );

        INSERT INTO cashier (name) (
        SELECT DISTINCT cashier_desc FROM temp_receipt WHERE temp_receipt.cashier_desc NOT IN 
        (select name from cashier)
        );

        INSERT INTO item_barcode ([desc]) (
        SELECT item_desc FROM temp_purchase WHERE temp_purchase.item_desc NOT IN 
        (select [desc] from item_barcode) GROUP BY item_desc
        );

SYC_LoopTemp_2Normal:将从临时购买和收据表中提取的记录插入到正常的收货和购买表中。这里不会提取重复/有缺陷的记录,而是提取另一对表。

select * into #receipt_normal from  receipt where 1 = 0 ;

    INSERT INTO #receipt_normal ([fileName], [pos_id],[recpt_id],[RefNo],[recpt_dt],[cashier_id],[customer_id],[customer_count],[total_sales],
               [cash_paid],[cash_change],[master_paid], [visa_paid], [debit_paid],[amex_paid],[TaxStdRate],[TaxStdTotal],[TaxStdAmnt],[refund], [ascii], [recpt_png])
    (
        SELECT t.[fileName], p.id, t.recpt_id, t.RefNo, t.recpt_dt, cashier.id, customer.id, t.customer_count, t.total_sales, 
            t.cash_paid, t.cash_change, t.master_paid, t.visa_paid, t.debit_paid, t.amex_paid, t.TaxStdRate, t.TaxStdTotal, t.TaxStdAmnt, t.refund, t.[ascii], t.[recpt_png]
        FROM ( SELECT * FROM temp_receipt where [fileName] in (
          select max([fileName]) from temp_receipt
          GROUP BY pos_desc,recpt_id, refund 
          HAVING COUNT(*) = 1 )) t, pos p, cashier, customer
        WHERE t.pos_desc = p.[desc] 
        AND t.cashier_desc = cashier.[name]
        AND t.customer_desc = customer.[name]
        AND ( SELECT COUNT(*) FROM receipt r, pos p
            WHERE r.[pos_id] = p.[id]
            AND p.[desc] = t.[pos_desc]
            AND r.[recpt_id] = t.[recpt_id]
            AND r.[refund] = t.[refund] ) = 0
    );

    DELETE FROM temp_receipt 
    WHERE [filename] IN ( SELECT [filename] FROM #receipt_normal);

    INSERT INTO receipt SELECT * FROM #receipt_normal;

    INSERT INTO purchase ([pos_id],[recpt_id],[salesPerson_id],[item_id],[count],[discount],[discRef_id],[fileName],[amount],[TaxCategory])
    (
        SELECT p.id, t.recpt_id, salesPerson.[id], bc.barcode, t.[count], t.[discount], discRef.[id], t.[fileName], t.[amount], t.TaxCategory
        FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
        WHERE t.item_desc = bc.[desc] 
        AND t.pos_desc = p.[desc] 
        AND t.salesPerson_desc = salesPerson.[name] 
        AND t.discRef = discRef.[desc] 
        AND t.fileName IN (SELECT fileName FROM #receipt_normal)
    );

    DELETE FROM temp_purchase
    WHERE [filename] IN (SELECT [filename] FROM #receipt_normal);

    DROP TABLE #receipt_normal;

SYC_LoopTemp_03Reprint:提取已在临时购买和收据表中标记为已转载或重复的记录,然后将它们放入重复的购买/收据表组中

BEGIN
DECLARE @_rcp_count INT;
    DECLARE @_rcp_loop INT;
    DECLARE @_pos_desc VARCHAR(max);
    DECLARE @_recpt_id VARCHAR(max);
    DECLARE @_refund INT;
    DECLARE @_sum_rows INT;
    DECLARE @_reprint_loop INT;

    DECLARE @_insert_fileName VARCHAR(max);
    DECLARE @_exist_fileName INT;
    DECLARE @_exist_total INT;
    DECLARE @_exist_purchase INT;
    DECLARE @_compare_total INT;
    DECLARE @_compare_purchase INT;

    DROP TABLE IF EXISTS uniqueRcp;
    CREATE TABLE uniqueRcp (
        [pos_desc] varchar(64) NOT NULL,
        [recpt_id] varchar(10) NOT NULL,
        [refund] smallint NOT NULL DEFAULT '0',
        PRIMARY KEY ([pos_desc],[recpt_id], [refund])
    );

    INSERT INTO uniqueRcp ([pos_desc],[recpt_id], [refund])
    (SELECT DISTINCT [pos_desc],[recpt_id], [refund] FROM temp_receipt);

    SET @_rcp_loop = 0;
    SELECT @_rcp_count = COUNT(*) FROM uniqueRcp;
    WHILE @_rcp_loop < @_rcp_count 
    BEGIN
        SELECT top 1 @_pos_desc = [pos_desc], @_recpt_id = [recpt_id], @_refund = [refund] 
        FROM uniqueRcp;

        DELETE TOP(1) FROM uniqueRcp;

        SELECT @_sum_rows = COUNT(*) FROM temp_receipt
            WHERE [pos_desc] = @_pos_desc
            AND [recpt_id] = @_recpt_id
            AND [refund] = @_refund;

        SET @_reprint_loop = 0;

        IF ( SELECT COUNT(*) FROM receipt r, pos p
            WHERE r.[pos_id] = p.[id]
            AND p.[desc] = @_pos_desc
            AND r.[recpt_id] = @_recpt_id
            AND r.[refund] = @_refund ) > 0
        BEGIN
            SELECT @_exist_total = max(r.total_sales), @_exist_purchase = COUNT(*)
            FROM receipt r, purchase b, pos p
            WHERE r.[fileName] = b.[fileName]
            AND r.[pos_id] = p.[id]
            AND p.[desc] = @_pos_desc
            AND r.[recpt_id] = @_recpt_id
            AND r.[refund] = @_refund
            GROUP BY r.[fileName];
        END 
        ELSE BEGIN
            SELECT top 1 @_insert_fileName = r.[fileName], @_exist_total = max(r.total_sales), @_exist_purchase = COUNT(*)
            FROM temp_receipt r, temp_purchase b
            WHERE r.[fileName] = b.[fileName]
            AND r.[pos_desc] = @_pos_desc
            AND r.[recpt_id] = @_recpt_id
            AND r.[refund] = @_refund
            GROUP BY r.[fileName]
            ORDER BY r.[fileName];
            /*ORDER BY r.recpt_dt; */

            SET @_reprint_loop = @_reprint_loop + 1;

            INSERT INTO receipt ([fileName], [pos_id],[recpt_id],[RefNo],[recpt_dt],[cashier_id],[customer_id],[customer_count],[total_sales],
            [cash_paid],[cash_change],[master_paid], [visa_paid], [debit_paid],[amex_paid],[TaxStdRate],[TaxStdTotal],[TaxStdAmnt],[refund], [ascii], [recpt_png])
            (
                SELECT t.[fileName], p.id, t.recpt_id, t.RefNo, t.recpt_dt, cashier.id, customer.id, t.customer_count, t.total_sales, 
                t.cash_paid, t.cash_change, t.master_paid, t.visa_paid, t.debit_paid, t.amex_paid, t.TaxStdRate, t.TaxStdTotal, t.TaxStdAmnt, t.refund, t.[ascii], t.[recpt_png]
                FROM temp_receipt t, pos p, cashier, customer
                WHERE t.pos_desc = p.[desc] 
                AND t.cashier_desc = cashier.[name]
                AND t.customer_desc = customer.[name]
                AND t.[fileName] = @_insert_fileName
            );
            INSERT INTO purchase ([pos_id],[recpt_id],[salesPerson_id],[item_id],[count],[discount],[discRef_id],[fileName],[amount],[TaxCategory])
            (
                SELECT p.id, t.recpt_id, salesPerson.[id], bc.barcode, t.[count], t.discount, discRef.[id], t.[fileName], t.[amount], t.TaxCategory
                FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
                WHERE t.item_desc = bc.[desc] 
                AND t.pos_desc = p.[desc] 
                AND t.salesPerson_desc = salesPerson.[name] 
                AND t.discRef = discRef.[desc] 
                AND t.[fileName] = @_insert_fileName
            );
            DELETE FROM temp_receipt
            WHERE [fileName] = @_insert_fileName;
            DELETE FROM temp_purchase
            WHERE [fileName] = @_insert_fileName;
        END;

        WHILE @_reprint_loop < @_sum_rows 
        BEGIN
            SELECT top 1 @_insert_fileName = r.[fileName], @_compare_total = max(r.total_sales), @_compare_purchase = COUNT(*)
            FROM temp_receipt r, temp_purchase b
            WHERE r.[fileName] = b.[fileName]
            AND r.[pos_desc] = @_pos_desc
            AND r.[recpt_id] = @_recpt_id
            AND r.[refund] = @_refund
            GROUP BY r.[fileName]
            ORDER BY r.[fileName];
            /*ORDER BY r.recpt_dt; */


            IF ( @_exist_total = @_compare_total AND @_exist_purchase = @_compare_purchase )
            BEGIN
                SELECT @_exist_fileName = COUNT(*)
                FROM dup_receipt_reprint
                WHERE [fileName] = @_insert_fileName;

                IF ( @_exist_fileName = 0 )
                BEGIN
                INSERT INTO dup_receipt_reprint ([fileName], [pos_id],[recpt_id],[RefNo],[recpt_dt],[cashier_id],[customer_id],[customer_count],[total_sales],
                    [cash_paid],[cash_change],[master_paid], [visa_paid], [debit_paid],[amex_paid],[TaxStdRate],[TaxStdTotal],[TaxStdAmnt],[refund], [ascii], [recpt_png])
                    (
                        SELECT t.[fileName], p.id, t.recpt_id, t.RefNo, t.recpt_dt, cashier.id, customer.id, t.customer_count, t.total_sales, 
                        t.cash_paid, t.cash_change, t.master_paid, t.visa_paid, t.debit_paid, t.amex_paid, t.TaxStdRate, t.TaxStdTotal, t.TaxStdAmnt, t.refund, t.[ascii], t.[recpt_png]
                        FROM temp_receipt t, pos p, cashier, customer
                        WHERE t.pos_desc = p.[desc] 
                        AND t.cashier_desc = cashier.[name]
                        AND t.customer_desc = customer.[name]
                        AND t.[fileName] = @_insert_fileName
                    );
                    INSERT INTO dup_purchase_reprint ([pos_id],[recpt_id],[salesPerson_id],[item_id],[count],[discount],[discRef_id],[fileName],[amount],[TaxCategory])
                    (
                        SELECT p.id, t.recpt_id, salesPerson.id, bc.barcode, t.[count], t.discount, discRef.id, t.[fileName], t.amount, t.TaxCategory
                        FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
                        WHERE t.item_desc = bc.[desc] 
                        AND t.pos_desc = p.[desc] 
                        AND t.salesPerson_desc = salesPerson.[name] 
                        AND t.discRef = discRef.[desc] 
                        AND t.[fileName] = @_insert_fileName
                    );
                END 

                DELETE FROM temp_receipt WHERE fileName = @_insert_fileName;
                DELETE FROM temp_purchase WHERE fileName = @_insert_fileName;
            END
            ELSE BEGIN
                    SELECT @_exist_fileName = COUNT(*)
                    FROM dup_receipt_different
                    WHERE [fileName] = @_insert_fileName;

                IF ( @_exist_fileName = 0 )
                BEGIN
                    INSERT INTO dup_receipt_different ([fileName], [pos_id],[recpt_id],[RefNo],[recpt_dt],[cashier_id],[customer_id],[customer_count],[total_sales],
                        [cash_paid],[cash_change],[master_paid], [visa_paid], [debit_paid],[amex_paid],[TaxStdRate],[TaxStdTotal],[TaxStdAmnt],[refund], [ascii], [recpt_png])
                    (SELECT t.[fileName], p.id, t.recpt_id, t.RefNo, t.recpt_dt, cashier.id, customer.id, t.customer_count, t.total_sales, 
                            t.cash_paid, t.cash_change, t.master_paid, t.visa_paid, t.debit_paid, t.amex_paid, t.TaxStdRate, t.TaxStdTotal, t.TaxStdAmnt, t.refund, t.[ascii], t.[recpt_png]
                            FROM temp_receipt t, pos p, cashier, customer
                            WHERE t.pos_desc = p.[desc] 
                            AND t.cashier_desc = cashier.[name]
                            AND t.customer_desc = customer.[name]
                            AND t.fileName = @_insert_fileName
                    );
                    INSERT INTO dup_purchase_different ([pos_id],[recpt_id],[salesPerson_id],[item_id],[count],[discount],[discRef_id],[fileName],[amount],[TaxCategory])
                    (
                        SELECT p.id, t.recpt_id, salesPerson.[id], bc.barcode, t.[count], t.discount, discRef.[id], t.[fileName], t.[amount], t.TaxCategory
                                FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
                                WHERE t.item_desc = bc.[desc] 
                                AND t.pos_desc = p.[desc] 
                                AND t.salesPerson_desc = salesPerson.[name] 
                                AND t.discRef = discRef.[desc] 
                                AND t.fileName = @_insert_fileName
                    );
                END 

                DELETE FROM temp_receipt WHERE fileName = @_insert_fileName;
                DELETE FROM temp_purchase WHERE fileName = @_insert_fileName;
            END 
            SET @_reprint_loop = @_reprint_loop + 1;
        END;
        SET @_rcp_loop = @_rcp_loop + 1;
    END;
END

SYC_LoopTemp_4Error:提取已标记为临时购买和收据表的错误记录,然后将其放入错误购买/收据表组中。

INSERT INTO dup_receipt_error ([fileName], pos_id,recpt_id,RefNo,recpt_dt,cashier_id,customer_id,customer_count,total_sales,
    cash_paid,cash_change,master_paid, visa_paid, debit_paid,amex_paid,TaxStdRate,TaxStdTotal,TaxStdAmnt,refund, [ascii], recpt_png)
    (
        SELECT t.[fileName], p.id, t.recpt_id, t.RefNo, t.recpt_dt, cashier.id, customer.id, t.customer_count, t.total_sales, 
      t.cash_paid, t.cash_change, t.master_paid, t.visa_paid, t.debit_paid, t.amex_paid, t.TaxStdRate, t.TaxStdTotal, t.TaxStdAmnt, t.refund, t.[ascii], t.recpt_png
        FROM temp_receipt t, pos p, cashier, customer
    WHERE t.pos_desc = p.[desc] 
    AND t.cashier_desc = cashier.[name]
    AND t.customer_desc = customer.[name]
    );

    INSERT INTO dup_purchase_error (pos_id,recpt_id,salesPerson_id,item_id,[count],discount,discRef_id,[fileName],amount,TaxCategory)
    (
        SELECT p.id, t.recpt_id, salesPerson.id, bc.barcode, t.[count], t.discount, discRef.id, t.fileName, t.amount, t.TaxCategory
    FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
    WHERE t.item_desc = bc.[desc] 
    AND t.pos_desc = p.[desc] 
    AND t.salesPerson_desc = salesPerson.[name] 
    AND t.discRef = discRef.[desc] 
        AND t.[fileName] IN ( SELECT [fileName] FROM temp_receipt )
    );  

    TRUNCATE table temp_receipt;
    DELETE FROM temp_purchase
    WHERE [fileName] IN (SELECT [fileName] FROM dup_receipt_error);

    INSERT INTO dup_purchase_leftover (pos_id,recpt_id,salesPerson_id,item_id,[count],discount,discRef_id,[fileName],amount,TaxCategory)
    (
        SELECT p.id, t.recpt_id, salesPerson.id, bc.barcode, t.[count], t.discount, discRef.id, t.fileName, t.amount, t.TaxCategory
    FROM temp_purchase t, item_barcode bc, pos p, salesPerson, discRef
    WHERE t.item_desc = bc.[desc]
    AND t.pos_desc = p.[desc] 
    AND t.salesPerson_desc = salesPerson.[name]
    AND t.discRef = discRef.[desc] 
    );

    TRUNCATE table temp_purchase;

按降序排列记录时间表;

date    _1New   _2Normal    _3Reprint   _4error
------------------------------------------------
2018-06-06 12:57:54 NULL    NULL    NULL    NULL
2018-06-06 12:56:44 NULL    NULL    NULL    NULL
2018-06-06 12:55:44 NULL    NULL    NULL    NULL
2018-05-29 11:52:40 1   1   1   1
2018-05-29 11:46:12 1   1   1   1
2018-05-29 11:43:52 1   1   1   1
2018-05-29 11:43:26 1   1   1   1
2018-05-29 11:40:56 1   1   1   1
2018-05-29 10:55:05 1   1   1   1

1 个答案:

答案 0 :(得分:0)

似乎我需要将@rundate标量变量声明为datetime2。 接下来,对于where条件,我将需要进行一些转换。因此,修改后的程序如下:

   execute SYC_LoopTemp_1New;
    UPDATE _SYC_LoopTemp_State SET _1New = 1 WHERE [date] = CONVERT([datetime2](3), @runDate);
    execute SYC_LoopTemp_2Normal;
    UPDATE _SYC_LoopTemp_State SET _2Normal = 1 WHERE [date] = CONVERT([datetime2](3), @runDate);
    execute SYC_LoopTemp_3Reprint;
    UPDATE _SYC_LoopTemp_State SET _3Reprint = 1 WHERE [date] = CONVERT([datetime2](3), @runDate);
    execute SYC_LoopTemp_4error;
    UPDATE _SYC_LoopTemp_State SET _4error = 1 WHERE [date] = CONVERT([datetime2](3), @runDate);