我正在尝试创建一个具有两个插入查询的简单存储过程。问题是它插入了从javafx桌面应用程序插入的同一记录的重复项。这是程序
ALTER PROCEDURE [dbo].[insertIntoInvoicesAndInvLines]
(@invoiceNumber AS NVARCHAR(MAX),
@supplierAccountCode AS NVARCHAR(MAX),
@theDate AS DATETIME,
@dealNoteNumber AS NVARCHAR(MAX),
@orderNumber AS VARCHAR(MAX),
@quantity AS FLOAT,
@units AS NVARCHAR(MAX),
@packSize AS FLOAT,
@cPaid AS BIT)
AS
BEGIN
INSERT INTO Invoices (C_invno, C_supno, D_invDate, C_delno, C_OrderNo, C_Paid)
VALUES (@invoiceNumber, @supplierAccountCode, @theDate, @dealNoteNumber, @orderNumber, @cPaid);
INSERT INTO InvLine (N_itemQty, C_ItemUnits, N_PkSize)
VALUES (@quantity, @units, @packSize);
END
答案 0 :(得分:1)
您可以在每个插入语句之前将条件添加到过程中。我假设发票编号列是唯一的。
有一个简单的解决方案:
IF NOT EXISTS(SELECT 1 FROM Invoices WHERE C_invno = @invoiceNumber)
BEGIN
-- insert into Invoices
END
但是您需要查看表结构-如果您想要数据库的一致性,则可以添加一些约束(例如唯一性)-如果您尝试插入一些重复项,则会产生错误。