以前一直用的Exec来执行存储过程,现在要改成sp_executesql这样来执行,看了不少博客,还是有点弄不懂,请大牛们指教,谢谢了。
原来的存储过程是这样的,如下:
- SQL code
Create procedure AddDataDB@strTable varchar(200),@strColl varchar(4000),@strValue ntext,@strWhere varchar(4000)=''asbegin if ISNULL(@strWhere,'')='' set @strWhere='' else set @[email protected] exec('insert [email protected]+N'([email protected]+N')values([email protected]+N')[email protected]) end
现在我改成sp_executesql这种形式的,报错了,
- SQL code
create proc TestSP as begin declare @strTable varchar(200),@strColl varchar(4000),@strValue ntext,@strWhere varchar(4000) declare @sql nvarchar(2000) set @strWhere='' if ISNULL(@strWhere,'')='' set @strWhere='' else set @[email protected] set @sql='insert [email protected]+N'([email protected]+N')values([email protected]+N')[email protected] execute sp_executesql @sql,[email protected] varchar(200),@strColl varchar(4000),@strValue ntext,@strWhere varchar(4000)' ,@strTable ,@strColl ,@strValue ,@strWhereend
请求懂的人帮帮忙,指教指教,谢谢了,,
------解决方案--------------------
- SQL code
create proc TestSP as begin declare @strTable varchar(200),@strColl varchar(4000),@strValue ntext,@strWhere nvarchar(4000) declare @sql nvarchar(2000) set @strWhere='' if ISNULL(@strWhere,'')='' set @strWhere='' else set @[email protected] set @sql=N'insert [email protected]+N'([email protected]+')values([email protected]+')[email protected] execute sp_executesql @sql,[email protected] varchar(200),@strColl varchar(4000),@strValue ntext,@strWhere nvarchar(4000)' ,@strTable ,@strColl ,@strValue ,@strWhereend
------解决方案--------------------
- SQL code
A. 执行简单的 SELECT 语句下面的示例创建并执行一个简单的 SELECT 语句,其中包含名为 @level 的嵌入参数。execute sp_executesql N'select * from pubs.dbo.employee where job_lvl = @level', [email protected] tinyint', @level = 35