求一条SQL存储过程和事务处理语句
我从变量中取得数据(ID) 类似(1,2,3,4,5,6,7,8)
下面是我写的存储过程,不知道怎么写才是正确的,求正确答案
存储 SQL 事务 行业数据
create procedure pro_DelModelList_sysrole
(
@ListId nvarchar(max) --即上面的id
)
as
begin transaction
declare @error int
declare @sql nvarchar(1000)
set @error = 0
set @sql = 'delete from tb_sys_role where id in ('+@ListId+')' --我想把这条语句中删除要么全部成功要么有一条没删除就全不删除
exec sp_ExecuteSql @sql
set @error = @error+ @@error
if @error <>0
begin
print '有错误,回滚'
rollback transaction
end
else
begin
PRINT '成功'
commit transaction
end
create procedure pro_DelModelList_sysrole
(
@ListId nvarchar(max) --即上面的id
)
as
begin transaction
declare @error int
set @error = 0
delete from tb_sys_role where CHARINDEX(','+cast(id as varchar)+',',','+@ListId+',')>0 --我想把这条语句中删除要么全部成功要么有一条没删除就全不删除
set @error = @error+ @@error
if @error <>0
begin
print '有错误,回滚'
rollback transaction
end
else
begin
PRINT '成功'
commit transaction
end
go