Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Sir,

I have some problem when implementing stored procedure in database.The following are my error described

SQL
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Student_Resource". The conflict occurred in database "DatabaseName", table "dbo.Resource", column 'ResourceId'.
The statement has been terminated.


Following is my stored procedure:-

SQL
USE [DatabaseName]
GO
/****** Object:  StoredProcedure [dbo].[insertupdatestudentdata]    Script Date: 04/13/2012 13:29:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <author,,name>
-- Create date: <create>
-- Description: <description,,>
-- =============================================





ALTER PROCEDURE [dbo].[insertupdatestudentdata] 
 -- Add the parameters for the stored procedure here
  @FirstName nvarchar(50),
  @LastName nvarchar(50),
  @UserName nvarchar(50),
  @Password nvarchar(50),
  @LevelId int,
  @TeamId int,
  @Active bit,
  @Flag char(1),
  @LastLoginDate datetime,
  @ClientId int,
 
  
  --@StudentId int,
  @StudentNumber int,
  @Address nvarchar(100),
  @Postcode nvarchar(20),
  @City nvarchar(30),
  @Phone nvarchar(20),
  @Mobile nvarchar(20),
  @EmailCompany nvarchar(30),
  @EmailPersonal nvarchar(30),
  @CreboId int,
  @FromYear nvarchar(10),
  @ToYear nvarchar(10),
  @ClassId int,
  @CoachId int,
  @CreatedBy int,
  @CreatedDate datetime,
  @ModifiedBy int,
  @ModifiedDate datetime,
  @Variant char(15),
  @CourseType nchar(3),
  @Remarks nvarchar(max),
  @ResetPassword int,
  @DefaultCompanyId int
 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 
 SET NOCOUNT ON;
  DECLARE @RetVal int;
 
      --SYTAX : exec myproc @a = @myvara out, @b = @myvarb out
      -- where @a is the variable where we store return value
      -- and @myvara is the out parameter  
      
 BEGIN TRAN insertStudentInfo

 EXEC insertresourceinfo @FirstName,@LastName,@UserName,@Password,@LevelId,@TeamId,@Active,@Flag,@LastLoginDate,@ClientId, @ResetPassword,
  @EmailPersonal,@CreatedBy,@CreatedDate,@ModifiedBy,@ModifiedDate,@DefaultCompanyId,@RetVal = @RetVal OUTPUT;
    
COMMIT TRAN insertStudentInfo;

--ALTER TABLE [DatabaseName].[dbo].[Resource] WITH NOCHECK add constraint [FK_Student_Resource] foreign key(ResourceId) references [SusanneDB].[dbo].[StudentDetails]
--on delete set null


 INSERT INTO [DatabaseName].[dbo].[StudentDetails]
   ([StudentId], [StudentNumber], [Address], [Postcode], [City], [Phone], [Mobile],
   [EmailCompany], [EmailPersonal], [CreboId], [FromYear], [ToYear], 
   [ClassId], [CoachId], [CreatedBy], [CreatedDate], [ModifiedBy], [ModifiedDate], [Variant], 
   [CourseType], [Remarks])
 VALUES
   (IDENT_CURRENT('Resource'),@StudentNumber, @Address,@Postcode, @City, @Phone, @Mobile, 
   @EmailCompany, @EmailPersonal, @CreboId, @FromYear , @ToYear, 
   @ClassId, @CoachId, @CreatedBy, @CreatedDate, @ModifiedBy, @ModifiedDate, @Variant, 
   @CourseType, @Remarks);
   
 SELECT @RetVal as studentid ,Email,Password FROM Resource WHERE ResourceId=@RetVal;
-- SELECT @RetVal as studentid;

END


Sir,On the StudentDetails tables that gives FK_Student_Resource.So please sir give me some solutions as soon as possible.
Posted
Updated 12-Apr-12 23:28pm
v2
Comments
ZurdoDev 13-Apr-12 8:25am    
What's the question? The error tells you what is wrong.

 
Share this answer
 
Whatever resource id you are trying to save in students table doesnt exist in Resources table.

I guess your student table have column ResourceId. But in the following query you didnt insert anything.


SQL
INSERT INTO [DatabaseName].[dbo].[StudentDetails]
   ([StudentId], [StudentNumber], [Address], [Postcode], [City], [Phone], [Mobile],
   [EmailCompany], [EmailPersonal], [CreboId], [FromYear], [ToYear],
   [ClassId], [CoachId], [CreatedBy], [CreatedDate], [ModifiedBy], [ModifiedDate], [Variant],
   [CourseType], [Remarks])
 VALUES
   (IDENT_CURRENT('Resource'),@StudentNumber, @Address,@Postcode, @City, @Phone, @Mobile,
   @EmailCompany, @EmailPersonal, @CreboId, @FromYear , @ToYear,
   @ClassId, @CoachId, @CreatedBy, @CreatedDate, @ModifiedBy, @ModifiedDate, @Variant,
   @CourseType, @Remarks);



So when you didnt mention the column name, it will take NULL as value which is not present in ResourceId column in Resource table.

So add ResourceId column also during insert.
 
Share this answer
 
The error indicates you need to put the ResourceID into the Resource table first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900