SP插入错误:与外键冲突

时间:2018-03-15 11:00:50

标签: sql asp.net stored-procedures

我知道有几个与此相关的问题,但我认为它不起作用。

我有一个sp将数据插入表中,该表也有外键。

这是我要插入的表格结构,

enter image description here

我使用asp.net和我的sp看起来如下

ALTER PROCEDURE [dbo].[spInsertJob]
@CompanyID INT,
@DepartmentID INT,
@No VARCHAR(50),
@Date DATETIME,
@CustomerID INT,
@JobTypeID INT,
@BillNo VARCHAR(50),
@GoodsType VARCHAR(50),
@Remarks VARCHAR(250),
@Cancelled BIT,
@CancelledRemarks VARCHAR(50),
@UserId INT,
@Closed BIT,
@Shipper VARCHAR(100),
@SupplierInvoice VARCHAR(50),
@HBillNo VARCHAR(50),
@JobStartedDate DATETIME,
@AssignedStaffid INT,
@VesselorFlightName VARCHAR(100),
@VesselorFlightArvDate DATETIME,
@FormNumber VARCHAR(50),
@FinancialId INT,
@NoofPackages INT,
@TypeOfPackages VARCHAR(100),
@ChargableWeight VARCHAR(50),
@Volume VARCHAR(50),
@DispatchMode VARCHAR(500),
@TransactionId INT OUT,
    @TransactionNo NVARCHAR(50) OUT

AS
BEGIN
     SET NOCOUNT ON;
     DECLARE @Id AS INT
       SELECT @Id=isnull(max(Id),0)+1 FROM  Job

 INSERT INTO Job( Id, CompanyID, DepartmentID, No, Date, CustomerID, JobTypeID, BillNo, GoodsType, Remarks, Cancelled, CancelledRemarks, UserId, Closed, Shipper, SupplierInvoiceNo, HBillNo, JobStartedDate, AssignedStaffid, VesselorFlightName, VesselorFlightArvDate, FormNumber,  NoOfPackages, TypeOfPackages, ChargableWeight, Volume, DispatchMode) 

values(@Id,@CompanyID,@DepartmentID,@No,@Date,@CustomerID,@JobTypeID,@BillNo,@GoodsType,@Remarks,@Cancelled,@CancelledRemarks,@UserId,@Closed,@Shipper,@SupplierInvoice,@HBillNo,@JobStartedDate,@AssignedStaffid,@VesselorFlightName,@VesselorFlightArvDate,   @FormNumber,@NoofPackages, @TypeOfPackages, @ChargableWeight, @Volume, @DispatchMode)

SELECT @TransactionId = @Id
SELECT @TransactionNo = @No 
SET NOCOUNT OFF;
END

我收到了错误

  

INSERT语句与FOREIGN KEY约束冲突" FK_Job_JobType"。冲突发生在数据库" dbname ",table" dbo.JobType"。
  声明已经终止。

这是我的JobType表

enter image description here

所以我试图像这样插入并且错误仍然是

INSERT INTO Job (Id,CompanyID,DepartmentID,No,Date,CustomerID,JobTypeID,BillNo,GoodsType,Remarks,Cancelled,CancelledRemarks,UserId,Closed,Shipper,SupplierInvoiceNo,HBillNo,JobStartedDate,AssignedStaffid,VesselorFlightName,VesselorFlightArvDate,FormNumber,NoOfPackages,TypeOfPackages,ChargableWeight,Volume,DispatchMode)

VALUES (88,1,0,'test',2018-01-01,13,1,3246,'','',0,'',2,0,'','',234632,2018-01-01,1,'test',2018-01-01,'',1,0,'','0','');

1 个答案:

答案 0 :(得分:0)

在您的插入查询中,正在插入JobTypeID作为''。如果是外键,则无法插入''进去。在其中输入有效的ID