首頁  >  文章  >  資料庫  >  SQL Server通过储存过程实现批量删除注意事项

SQL Server通过储存过程实现批量删除注意事项

WBOY
WBOY原創
2016-06-07 17:58:081072瀏覽

这里设定传过来的参数是拼接好的字符串,如:1,2,3,4,5 create procedure up_batchDeleteById ( @condition varchar(max) ) as delete from dt_name where id in(@condition) 以上的做法看似正确,实际会报错,具体原因是说id是int类型的,而@condition是字

 
这里设定传过来的参数是拼接好的字符串,如:1,2,3,4,5

create procedure up_batchDeleteById

(

    @condition varchar(max)

)

as

delete from dt_name where id in(@condition)

以上的做法看似正确,实际会报错,具体原因是说id是int类型的,而@condition是字符串类型,这样无法删除

正确做法:

create procedure up_batchDeleteById

(

    @condition varchar(max)

)

as

declare @sql varchar(max)

set @sql='delete from dt_name where id in (' + @condition + ')'

exec(@sql)
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn