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
@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
存储过程一结束,临时表就死亡了。你也可以在存储过程最后显式删除:drop table #tmp
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