修改内容的存储过程

来源:互联网 发布:淘宝退货率计算 编辑:程序博客网 时间:2024/05/27 14:14


  set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[updateNews]
@newsid int,
@title nvarchar(100),
@content text,
@catids varchar(200),
@error nvarchar(200) output
as
begin
declare @index int
declare @catid varchar(10)

begin transaction
begin try
       update newsContent set title=@title,[content]=@content
       where id=@newsid

        delete from NewsCategory where newsid=@newsid
--while循环
while(len(@catids)>0)
begin
   set @index=charindex(':',@catids)
 
   if(@index<>0)
      begin
        set @catid=substring(@catids,1,@index-1)
        set @catids=substring(@catids,@index+1,len(@catids)-@index)
      end
   else
      begin
        set  @catid=@catids
        set @catids=''
      end
insert into newscategory(newsid,catid) values(@newsid,cast(@catid as int))
end
--循环结束
commit transaction
return 0
end try
begin catch
     set @error=error_message()
     rollback transaction
     return -1
end catch
end