2010-09-27 145 views
0

我的存儲過程是 -插入語句與外鍵約束衝突?

CREATE PROCEDURE [dbo].[usp_SetMenu](  
    @locationId BIGINT,  
    @menuId BIGINT = NULL,  
    @name VARCHAR(100) = NULL,  
    @taxable BIT = NULL, 
    @type VARCHAR(100) = NULL, 
    @dateFrom DATETIME = NULL,  
    @dateTo DATETIME = NULL,  
    @timeFrom VARCHAR(10) = NULL,  
    @timeTo VARCHAR(10) = NULL,  
    @price MONEY = NULL,  
    @discountPerc FLOAT = NULL,  
    @discTimeFrom VARCHAR(10) = NULL,  
    @discTimeTo VARCHAR(10) = NULL,  
    @textcolor varchar(10) = null, 
    @bodycolor varchar(10) = null, 
    @createdBy BIGINT = NULL,  
    @createdOn DATETIME = NULL,  
    @modifiedBy BIGINT = NULL,  
    @modifiedOn DATETIME = NULL,  
    @menuProductsXML NTEXT = NULL , 
    @IsCopy VARCHAR (10) = NULL, 
    @CopyMenuId BIGINT = NULL, 
    @menuTaxXML NTEXT = NULL , 
    @menuExists INT = NULL OUTPUT, 
    @newMenuId  INT = NULL OUTPUT 
    )   
    AS 
SET NOCOUNT ON   

---------------------------------------------------------------------   
-- Declarations of variables   
---------------------------------------------------------------------   
DECLARE @ptrHandle INT  
---------------------------------------------------------------------   
-- initialize variables   
---------------------------------------------------------------------   

---------------------------------------------------------------------   
-- get the data   
---------------------------------------------------------------------   
IF(@menuId IS NULL) -- If menuid is null then create a new record  
BEGIN  

    select @menuExists = count('x') from tblMenu 
    where [name] = @name and isDeleted = 0 and [email protected] 

    if @menuExists > 0 
Return 

    INSERT INTO tblMenu  
(locationid  
,[name]  
,[type] 
,taxable  
,datefrom  
,dateto  
,timefrom  
,timeto  
,price  
,discountperc  
,disctimefrom  
,disctimeto  
,bodycolor 
,textcolor 
,createdby  
,createdon)  
    VALUES  
(@locationId  
,@name  
,@type 
,@taxable  
,@dateFrom  
,@dateTo  
,@timeFrom  
,@timeTo  
,@price  
,@discountPerc  
,@discTimeFrom  
,@discTimeTo  
,@bodycolor 
,@textcolor 
,@createdBy  
,@createdOn)  

    SET @menuId = @@IDENTITY 

END  
ELSE  -- If menuid is not null then update that record  

    select @menuExists = count('x') from tblMenu 
    where [name] = @name and MenuId <> @menuId and isDeleted = 0 and [email protected] 

    if @menuExists > 0 
Return 

    UPDATE tblMenu  
    SET locationid = @locationId  
    ,[name] = @name  
    ,[type] = @type  
,taxable = @taxable 
    ,datefrom = @dateFrom  
    ,dateto = @dateTo  
    ,timefrom = @timeFrom  
    ,timeto = @timeTo  
    ,price = @price  
    ,discountperc = @discountPerc  
    ,disctimefrom = @discTimeFrom  
    ,disctimeto = @discTimeTo 
,bodycolor = @bodycolor 
,textcolor = @textcolor 
    ,modifiedby = @modifiedBy  
    ,modifiedon = @modifiedOn  
    WHERE menuid = @menuId  

-- if menu product collection is passed then insert new records  
IF(@menuProductsXML IS NOT NULL)  
BEGIN  
-- Clearing the old menu products and inserting new ones  

    DELETE tblMenuProduct WHERE menuid = @menuId  

    EXEC sp_xml_preparedocument @ptrHandle OUTPUT, @menuProductsXML  
    INSERT INTO tblMenuProduct  
     (menuid  
     ,productid 
    ,categoryid 
     ,productprice  
     ,createdby  
     ,createdon)  
    SELECT @menuId,  
    ProductId,  
    CategoryId, 
    ProductPrice,  
    @createdBy,  
    @createdOn  
    FROM OPENXML (@ptrHandle, '/ArrayOfMenuProductEntity/MenuProductEntity', 2)  
    WITH(ProductId BIGINT,CategoryId BIGINT, ProductPrice MONEY)  
END 
    if(@IsCopy = 'True') 
Begin 
    INSERT INTO tblMenuProduct  
     (menuid  
     ,productid 
    ,categoryid 
     ,productprice  
     ,createdby  
     ,createdon)  
    Select @menuId,productid,categoryid,productprice,@createdBy,@createdOn 
    From tblMenuProduct where menuid = @CopyMenuId 

    SET @newMenuId = @menuId 
End 
IF(@menuTaxXML IS NOT NULL)  
BEGIN  
    DELETE tblMenuTaxClass WHERE menuid = @menuId  

    EXEC sp_xml_preparedocument @ptrHandle OUTPUT, @menuTaxXML  
    INSERT INTO tblMenuTaxClass  
     (menuid  
     ,taxclassid  
     )  
    SELECT @menuId,  
    TaxClassId 
    FROM OPENXML (@ptrHandle, '/ArrayOfTaxClassEntity/TaxClassEntity', 2)  
    WITH(TaxClassId BIGINT)  

END  
---------------------------------------------------------------------   
-- exit the sproc   
---------------------------------------------------------------------   

-----------------------------------------------------------------------------------------------------------------------------   

SET NOCOUNT OFF   

END 

例外:insert語句衝突與外鍵約束
Why I am getting this exception and how can I fix this?

+0

什麼是完整的錯誤消息?它應該告訴你哪個表失敗了'tblMenu','tblMenuProduct','tblMenuTaxClass'。也可以使用'SCOPE_IDENTITY'而不是'@@ Identity'。如果你在表上有任何觸發器,那麼'MenuId'的值可能不是你所期望的。這可以解釋這個錯誤。 – 2010-09-27 07:00:56

回答

3

的主鍵值不會成爲there.you試圖插入Foreign key值相應的PK不會在那裏的表。

table1的

ID(PK) 
1 
2 
3 

表2

ID1(PK) ID(FK) 
1   1 
2   1 
3   4// Error not there in PK table