用户表 table1 有两个列col1,col2
我想生成这样一条语句
select col1,col2 from table1数据库的每个用户表都生成这样一条sql 语句。
请给出sql 语句,以前见过,没找到,自己不想写了。谢谢。

解决方案 »

  1.   

    EXEC sp_MSforeachtable @command1="print '?'",
             @command2= "select col1,col2 from ? "
      

  2.   

    sp_Mmsforeachtable @command1="select 'select col1,col2 from ? '"
      

  3.   

    create table table1
    (
    col1 varchar(5),
    col2 varchar(5)
    )
    insert into table1
    select 'q','2' union all
    select '2','s'
    go 
    declare @s varchar(50)
    set @s=''
    select @s=@s+','+name from syscolumns where id=object_id('table1')
    set @s=stuff(@s,1,1,'')
    exec( 'select '+@s+' from table1')
    col1  col2
    ----- -----
    q     2
    2     s(2 行受影响)
      

  4.   

    EXEC sp_msforeachtable @command1="select 'select col1,col2 from ? '"
      

  5.   

    col1,col2只是举个例子。是 select 该表的每个列 from 该表
      

  6.   

    sp_msforeachtable @command1="declare @a varchar(100);select @a=isnull(@a+',','')+name from syscolumns where id=object_id('?');select 'select '+@a+' from ?'
    "
      

  7.   

    换成select *就可以了
    EXEC sp_MSforeachtable @command1="print '?'",
             @command2= "select * from ? "
      

  8.   

    create table table1
    (
    col1 varchar(5),
    col2 varchar(5)
    )
    insert into table1
    select 'q','2' union all
    select '2','s'
    go 
    declare @s varchar(50)
    set @s=''
    select @s=@s+','+name from syscolumns where id=object_id('table1')
    set @s=stuff(@s,1,1,'')
    print @s