@tmp 改为 #tmp用临时表

解决方案 »

  1.   

    动态SQL内部不能访问外部的变量——包括表类型变量,作用域不同。
      

  2.   

    create procedure GetTask
    @where nvarChar(50) = NULL
    asbegincreate table #tmp Table(ID int, Year int)insert #tmp select col1, col2 from [任意返回两列值的表]declare @Sql nvarchar(max)
    if @where is not null
    set @Sql = 'select * from #tmp where ' + @whereEXEC sp_executesql @Sql, .........end
      

  3.   


    存储过程一结束,临时表就死亡了。你也可以在存储过程最后显式删除:drop table #tmp
      

  4.   

    when stored procedure excute end, system will drop temp table
      

  5.   

    建立了临时表后,如果存储过程出现异常,还没有执行到drop table #tmp这句,那么临时表能自动删除吗?
      

  6.   


    create table #tmp(ID int, Year int)declare @Sql nvarchar(300)
    set @Sql = 'select * from #tmp'--若条件不为null
    if @where IS NOT NULL
    set @Sql = @Sql + @Where--若排序不为null
    if @Order IS NOT NULL
    set @Sql = @Sql + @Orderexec @Sql这样写存储过程是有问题的:找不到存储过程 'select * from #tmp
      

  7.   

    exec (@Sql)