高手请进,动态执行SQL语句问题以及sp_executesql的用法
DECLARE @recordcount int
DECLARE @dir varchar(10)
EXEC( 'SELECT @recordcount=(SELECT COUNT(1) FROM tblTopic WHERE Dir LIKE ' '% ' + @dir + '% ' ') ')
提示未声明的变量@recordcount
我查一下些贴子说不在同一作用域,使用sp_executesql过程可以解决,但实在看不懂sp_executesql和N '的用法,请高手帮忙解释一下。
[解决办法]
DECLARE @recordcount int
DECLARE @dir varchar(10),
@Sql nvarchar(4000)
Set @Sql=N 'SELECT @recordcount=(SELECT COUNT(1) FROM tblTopic WHERE Dir LIKE ' '% ' + @dir + '% ' ') '
exec sp_executesql @Sql,N '@recordcount int out ',@recordcount out
[解决办法]
fa_ge(鶴嘯九天) ( ) 信誉:100 2007-09-04 10:11:35 得分: 0
DECLARE @recordcount int
DECLARE @dir varchar(10)
declare @s varchar(100)
set @s= 'SELECT '+@recordcount+ '=(SELECT COUNT(1) FROM tblTopic
WHERE Dir LIKE ' '% ' + @dir + '% ' ') ' '
exec(@s)
-------------
怎麼還犯同樣的錯誤?