本文介绍了插入完成后如何更新...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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







请帮助我

当我在上面的程序中插入新值时工作正常但返回类型是存在CSprefix没有更新......




Pls help me
When am insert new values above procedure working fine but the ReturnsType is exists CSprefix not updated ......

推荐答案


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

使其更加明显。



但是你所要求的是没有意义的 - 如果没有记录,你的SP会执行INSERT, 如果已经存在则执行UPDATE。 />


但你的插入看起来使用IDENTITY字段 - 在这种情况下你不能提供ID值(所以可能会传递一个负值以确保它插入)。如果它插入,那么更新没有意义,因为它更新你刚刚插入的具有相同值的字段正在更新...



我觉得你需要坐下来思考你想要做什么,因为我很确定你要求的不是它......

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...


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...


这篇关于插入完成后如何更新...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:23