Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends

The following is my stored procedure for insert and update query

alter procedure sp_insertsection(@sectionid int,@sectionname char(10),@classname varchar(30),@acyear varchar(30),@datetime datetime,@adminid int,@medium varchar(20))
as
begin
if exists(select * from schoolcampus.dbo.Section_details where SectionId=@sectionid and SectionName=@sectionname)
begin
update  schoolcampus.dbo.Section_details set SectionName=@sectionname,ClassName=@classname,AcademicYear=@acyear,Medium=@medium,Updateddatetime=@datetime,adminid=@adminid where SectionId=@sectionid
end
else
begin
insert into schoolcampus.dbo.Section_details(SectionId,SectionName,className,AcademicYear,Createddatetime,adminid,Medium) values(@sectionid,@sectionname,@classname,@acyear,@datetime,@adminid,@medium)
end
end

execution part
exec sp_insertsection 51,'A','I','2013-2014','07-17-2013 12:35:000',5002,'Tamil'

The above error is occured when executing the stored procedure.
Posted
Updated 16-Jul-13 21:41pm
v2
Comments
Raja Sekhar S 17-Jul-13 3:53am    
You are encountering this error because.. you are trying to insert a duplicate value into the Primary key column of the table.... Provide the MetaData of the table... or in that
IF EXISTS statement u can use the statement based on Primary key column... like
If exists(Select * from TableName Where Primarycolumnvalue=@Param).
If that exists statement should not be changed.... then Provide the details of the table so we can help...

In your case if you are trying to insert into SectionId and SectionName like below:

SectionId, SectionName
51, A
51, B
.....

then, SectionId and SectionName fields should not be unique, as you are trying to create a composite primary key as per you query:

SQL
if exists(select * from schoolcampus.dbo.Section_details where SectionId=@sectionid and SectionName=@sectionname)
 
Share this answer
 
From the error, I guess that SectionId is the PrimaryKey of Table Section_details.

Your Update statement is correct as you are updating a particular Section after getting it (if it exists).

If it doesn't exist, you need to insert all the details, but you can't insert value to the SecionId as it is the PrimaryKey. That's why it is throwing exception that there is already a Section with Id 51.
SQL
insert into schoolcampus.dbo.Section_details(SectionId,SectionName,className,AcademicYear,Createddatetime,adminid,Medium) values(@sectionid,@sectionname,@classname,@acyear,@datetime,@adminid,@medium)


So, remove the SectionId insertion from this query. And after that it should run without any errors.
SQL
insert into schoolcampus.dbo.Section_details(SectionName,className,AcademicYear,Createddatetime,adminid,Medium) values(@sectionname,@classname,@acyear,@datetime,@adminid,@medium)
 
Share this answer
 
Comments
Raja Sekhar S 17-Jul-13 4:11am    
@Tadit Dash...
If SectionId is PrimaryKey Then your Query will Throw an error..
Because PrimaryKey Columns won't allow nulls...
Refer this Link: http://msdn.microsoft.com/en-us/library/ms189039.aspx
Hello Raja Sekhar S,

I added answer assuming that it is a Autoincreamented and Identity On Primary Key.
Else when OP will reply, I will modify my solution. Thanks...

Regards,
Tadit
Raja Sekhar S 17-Jul-13 4:27am    
Sure... am not the one who DownVoted it...
Oh that's completely fine. I am not complaining either... :)

Everybody has the rights here to Downvote and Upvote, so no problems...

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