select a1=case name='a1' then id end,a2=case name='a2' then id end from [Table Item]

解决方案 »

  1.   

    declare @a varchar(100)set @a=(select Name from table_Item where xxxx )exec ('select * from '+@a+'where xxxxxxx')xxxx处自己填写条件
      

  2.   

    select a1=case name='a1' then id end,a2=case name='a2' then id end from [Table Item]
      

  3.   

    exec ('select * from '+@a+' where xxxxxxx')
                              ^
    补一个空格
      

  4.   

    create table t(Id int,Name varchar(10))
    insert into t select 1,'A1'
    union all select 2,'A2'
    gocreate table A1(id int)
    create table A2(id int)
    gocreate proc test
    asdeclare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+char(13)+'select * from '+name from t
    set @sql=stuff(@sql,1,1,'')
    exec(@sql)
    go
    exec testdrop table t,A1,A2
    drop proc test--动态SQL