在SQL语句中,我们有时会因批量删除而用到in语句,如delete from table where field in (1,2,3),一般在存储过程中我们会写成delete from table where field in (@field),但现在出现一个问题,@field到底是什么类型,如何去写,我们得用sp_executesql,此句应该是对SQL语句执行替换,也就是将参数原封不动的替换到SQL语句中,然后再执行
实例如下:
CREATE PROCEDURE an_AccountList_Delete @al_id nvarchar(80)
AS
declare @tt nvarchar(800)
set @tt='delete from an_AccountList where al_id in ('+@al_id+')'
exec sp_executesql @tt
GO