下面有错误
declare @table table(id int,name nvarchar(30))
insert into @table 
select 1,'a' union all 
select 2,'b' union all 
select 3,'c'
-----------------------
declare @strSQL nvarchar(100)
set @strSQL='select * from '+@table
exec(@strSQL)
go

解决方案 »

  1.   

    declare @table table(id int,name nvarchar(30))
    insert into @table 
    select 1,'a' union all 
    select 2,'b' union all 
    select 3,'c'select * from @table
    这样不就可以了嘛
      

  2.   

    set @strSQL='select * from ' + @table 里面的@table 应该是字符变量
      

  3.   

    declare @strSQL nvarchar(1000)
    set @strSQL='
    declare @table table(id int,name nvarchar(30))
    insert into @table 
    select 1,''a'' union all 
    select 2,''b'' union all 
    select 3,''c''select * from @table'
    exec(@strSQL)
    go
      

  4.   

    上面的不行,
    需要首先生成一个表变量,然后再动态构造sql语句执行.
      

  5.   

    declare @strSQL nvarchar(1000)
    set @strSQL=''
    declare @table table(id int,name nvarchar(30))
    insert into @table 
    select 1,'a' union all 
    select 2,'b' union all 
    select 3,'c'select * from @table
    exec(@strSQL)
    go
      

  6.   

    @table就是表变量呀,可以直接:
    select * from @table为何要用动态sql语句呢?
    如果必用动态sql的话,要写成这样:
    set @str='select * from @table'
      

  7.   

    DECLARE @table
    SET @table='SELECT * FROM .........'
    EXEC SP_EXECUTESQL @table
    @table必须是 NVARCHAR TEXT NTEXT 类型
    要是不好使称把命给你
      

  8.   

    表变量是不可以的必须声明为临时表 create table #table1(...)