exec('select 1 from '+@tn+'stat where tatmonth='+@statmonth+' and sn='+@sn) 
if @@rowcount<1
   print ('没有检索出语句!')
else
   print ('检索出'+@@rowcount+'条语句!')

解决方案 »

  1.   

    print 'select * from '+@tn+'stat where tatmonth='+@statmonth+' and sn='+@sn
    看看这句是什么内容?
    再将其复制到查询分析器中单独执行,看看错在那里
      

  2.   

    如果你的tatmonth和sn字段是字符型,那么
    exec('select 1 from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''') 
    if @@rowcount<1
       print ('没有检索出语句!')
    else
       print ('检索出'+@@rowcount+'条语句!')
      

  3.   

    最好事先置@@rowcount为0
    set @@rowcount=0
      

  4.   

    @statmonth 、@sn是字符串变量才能那样写,否则要类型转换
      

  5.   

    TRY:
    if exists ( exec('select * from '+@tn+'stat where tatmonth='''+@statmonth+''' and sn='''+@sn+'''')  )
      

  6.   

    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 '无'
      

  7.   

    @tn+'stat 寫法好像是有錯!! 是不是想動態傳一table 名字
    如是寫成;+rtrim(@tn)+'stat.............
      

  8.   

    语法
    EXISTS subqueryEXEC() 不是子查询
     
    我认为是概念混淆
      

  9.   

    @statmonth 、@sn都是字符串变量,我只是想动态传一下TABLE的名字,因为表名和字段名有联系,我是想列用表和字段的联系写一下通用的触发器......