Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
create Proc insert_Returns
@RID int output,
@ReturnsType int,
@CSPrefix varchar(50)
AS
begin
if not exists(select ReturnsType from tbl_ReturnSetings where RID=@RID)
begin 
INSERT [dbo].[tbl_ReturnSetings]
(
	[ReturnsType],
	[CSPrefix]
)
VALUES
(
	@ReturnsType,
	@CSPrefix
)
	SELECT @RID=SCOPE_IDENTITY();
	end
	else
	begin
	update tbl_ReturnSettings
	 SET	CSPrefix = @CSPrefix 
	where RID=@RID
end
end



Pls help me
When am insert new values above procedure working fine but the ReturnsType is exists CSprefix not updated ......
Posted
Updated 10-Jul-13 5:10am
v5
Comments
[no name] 10-Jul-13 8:19am    
Help you with what? This is just a code dump, not a question or description of any kind of a problem.
ZurdoDev 10-Jul-13 8:27am    
What's your question? How can we help?
Amarender1479 10-Jul-13 8:31am    
when am insert into table its working if ReturnsType is exists it is not updated
ZurdoDev 10-Jul-13 8:39am    
I realize English may not be your first language but I cannot understand what you just said.
ZurdoDev 10-Jul-13 8:40am    
Why is @RID set as output? Don't do that if you are wanting to use it in your where statement.

As you have not described any details regarding the problem,i assume you want to update something after completion of insert operation.If this is so,you can make use of After Insert Trigger.Refer to below link for reference

Triggers in Sql Server[^]

Regards.. :laugh:
 
Share this answer
 
Re-indenting your code so it is clearer:
SQL
create Proc insert_Returns
@RID int output,
@ReturnsType int,
@CSPrefix varchar(50)
AS
begin
   if not exists(select ReturnsType from tbl_ReturnSetings where RID=@RID)
   begin 
      INSERT [dbo].[tbl_ReturnSetings](
	[ReturnsType],
	[CSPrefix])
      VALUES(
	@ReturnsType,
	@CSPrefix)
      SELECT @RID=SCOPE_IDENTITY();
   end
   else
   begin
      UPDATE tbl_ReturnSettings
      SET CSPrefix = @CSPrefix 
      WHERE RID=@RID
   end
end
Makes it a bit more obvious.

But what you are asking for is pointless - your SP does an INSERT if no record exists, or does an UPDATE if it is already present.

But your insert looks to use an IDENTITY field - in which case you cannot provide an ID value (So are presumably passing a negative value to ensure it does an insert). And if it does an insert, then there is no point in doing the update, because it updates the field you just inserted with the same value are are updating...

I think you need to sit down and think about what you are trying to do, because I'm pretty sure what you have asked for is not it...
 
Share this answer
 
Comments
Amarender1479 10-Jul-13 8:38am    
if recoed exists it is not updated
OriginalGriff 10-Jul-13 8:55am    
What values did you pass to the SP? how did you pass them? How did you check if the record was updated?
why are you setting @RID output when you are using it in your where statement.
don't you think you should pass it to you SP.

Try below code..


create Proc insert_Returns
@RID int,
@ReturnsType int,
@CSPrefix varchar(50)
AS
begin
   if not exists(select ReturnsType from tbl_ReturnSetings where RID=@RID)
   begin 
      INSERT [dbo].[tbl_ReturnSetings](
	[ReturnsType],
	[CSPrefix])
      VALUES(
	@ReturnsType,
	@CSPrefix)
      SELECT @RID=SCOPE_IDENTITY();
   end
   else
   begin
      UPDATE tbl_ReturnSettings
      SET CSPrefix = @CSPrefix 
      WHERE RID=@RID
   end
end


I think now it should work fine for you...
 
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