从sysobjects表里取出一个表
然后从这表里取出数据
用一句SQL文怎么实现呢?例如:
select * from (select name from sysobjects where id='123456')当然上面这个例子不能实现
我就是想要这种样式的不知说明白了没有
高人指点。

解决方案 »

  1.   

    declare @name varchar(50),@strSQL varchar(500)
    select @name=name from sysobjects where id='123456'
    set @strSQL='select * from '+@name 
    sp_executesql @strSQL
      

  2.   

    谢谢楼上高人,
    不过我的意思是说用一句SQL文实现
    不能写存储过程的
    项目要求的。
      

  3.   

    sp_executesql 'select * from '+(select name from sysobjects where id='123456' )
      

  4.   

    再来解释一下吧
    不能用sp_execute
    就是写一句能直接在分析器里能运行的SQL文
      

  5.   

    create table tb(id int,name varchar(50))
    insert into tb select 1,'a'
    insert into tb select 2,'b'declare @sql varchar(8000),@tbid varchar(9)
    set @tbid=object_id('tb')if exists(select name from sysobjects where id=@tbid)
    begin
    set @sql='select * from ['+(select name from sysobjects where id=@tbid)+']'
    exec(@sql)
    end
    id name
    1 a
    2 b一句不能
      

  6.   

    exec('select * from ['+(select name from sysobjects where id=123456)+']')
      

  7.   

    有没有把取出来的数据转换成对象的函数?
    例如
    select * from FuncXXXX((select name from sysobjects where id='123456'))
    通过那个函数把取出来的对象变成表对象?
      

  8.   

    呵呵
    快来帮忙啊
    着急ing
      

  9.   

    create table tb(id int,name varchar(50))
    insert into tb select 1,'a'
    insert into tb select 2,'b'select object_id('tb')
    --975342539--一句
    sp_MSforeachtable @command1='select * from ?' , @whereand=' and object_id(o.name)=975342539'id name
    1 a
    2 b非要一句有啥用啊?
      

  10.   

    客户自己的一个开发工具
    把东西都封装起来了
    留了填写标准SQL文的处理的功能
    所以只能填写标准查询,插入,删除,更新处理语句。
    无奈
      

  11.   

    可以调用存储过程么?
    存储过程也是一个标准的SQL啊,
      

  12.   

    另外 12 楼高人
    你那个命令在在哪儿找到的
    为什么我在SQL的联机文档里找不到呢?谢谢。
      

  13.   

    sp_MSforeachtable
    ------------
    这个是未公开的存储过程,
    就是遍历整个数据库的表执行操作,
    http://blog.csdn.net/abandonship/archive/2008/06/06/2515943.aspx
      

  14.   

    那存储过程是没有说明的,但可以使用Exec sp_helptext sp_MSforeachtable
    Exec sp_helptext sp_MSforeach_worker来查询看具体的内容。
      

  15.   

    select object_id('tb') 
    --975342539 --一句 
    sp_MSforeachtable @command1='select * from ?' , @whereand=' and object_id(o.name)=975342539' 上边两句能否合成一句?谢谢
      

  16.   

    --使用动态sql
    declare @tablename varchar(20)
    declare @sql varchar(1000)
    select @tablename=name from sysobjects where id='123456'set @sql ='select * from '+@tablename
    exec (@sql)
      

  17.   

    exec sp_MSforeachtable @command1='select * from ?' , @whereand=' and object_id(o.name)=object_id(''tb'')'
      

  18.   


    我考,果然是高手,学习了
    还想学习一下
    如果不知道那个tb表名
    只知道一部分
    可不可以用select name from sysobjects where name like '%b' 类似这样的语句替换最后的object_id(''tb'')呢回头给你再加分,谢谢。
      

  19.   

    exec sp_MSforeachtable @command1='select * from ?' , 
    @whereand=' and object_id(o.name) in(select object_id(name) from sysobjects where name like ''%b'')' 
      

  20.   

    OK
    搞定
    这样就可以了sp_MSforeachtable @command1='select * from ?' , @whereand=' and o.name like ''%b'''