insert语句与外键约束冲突?

时间:2010-09-27 06:40:57

标签: asp.net sql-server-2005 stored-procedures

我的存储过程是 -

    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 locationid=@locationId  

  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 locationid=@locationId  

  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  

例外:插入语句与外键约束冲突
Why I am getting this exception and how can I fix this?

1 个答案:

答案 0 :(得分:3)

主键值不会出现在那里。您试图将Foreign key值插入到对应PK不会出现的表中。

表1

ID(PK)
1
2
3

表2

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