В настоящее время у меня есть хранимая процедура, известная как процедура основного входа, которая обрабатывается следующим образом:
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
) как ноль, тогда как, если я запускаю команду обновления как один отдельный оператор SQL, он может быть обновлен.Самое смешное, что за несколько дней до того, как произошел этот инцидент, значения в записи правильно отображают значение 1 в четырех столбцах.
Я проверил остальные четыре хранимых процедуры, и они работали правильно,то есть никаких ошибок не было показано, когда они работали.
Есть ли какая-либо часть процедуры или целевой таблицы, которая является ошибочной, или это может быть связано с тем, что процесс BCP не был завершен полностью?
Заранее спасибо.
ОБНОВЛЕНИЕ: Да, процедуры запускаются через BCP.Я также использую SSMS, если я хочу запустить одну из 4 внутренних процедур.
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;
Расположение в порядке убывания для таблицы record_time;
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