print 'select * from '+@tn+'stat where tatmonth='+@statmonth+' and sn='+@sn 看看这句是什么内容? 再将其复制到查询分析器中单独执行,看看错在那里
如果你的tatmonth和sn字段是字符型,那么 exec('select 1 from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''') if @@rowcount<1 print ('没有检索出语句!') else print ('检索出'+@@rowcount+'条语句!')
最好事先置@@rowcount为0 set @@rowcount=0
@statmonth 、@sn是字符串变量才能那样写,否则要类型转换
TRY: if exists ( exec('select * from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''') )
declare @sql nvarchar(4000),@结果 int set @sql=N'select @a=1 from '+@tn+'stat where tatmonth='+@statmonth+' and sn='+@sn exec sp_executesql @sql,N'@a int output',@结果 output if @结果 is not null select '有' else select '无'
看看这句是什么内容?
再将其复制到查询分析器中单独执行,看看错在那里
exec('select 1 from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''')
if @@rowcount<1
print ('没有检索出语句!')
else
print ('检索出'+@@rowcount+'条语句!')
set @@rowcount=0
if exists ( exec('select * from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''') )
set @sql=N'select @a=1 from '+@tn+'stat where tatmonth='+@statmonth+' and sn='+@sn
exec sp_executesql @sql,N'@a int output',@结果 output
if @结果 is not null
select '有'
else
select '无'
如是寫成;+rtrim(@tn)+'stat.............
EXISTS subqueryEXEC() 不是子查询
我认为是概念混淆