从sysobjects表里取出一个表
然后从这表里取出数据
用一句SQL文怎么实现呢?例如:
select * from (select name from sysobjects where id='123456')当然上面这个例子不能实现
我就是想要这种样式的不知说明白了没有
高人指点。
然后从这表里取出数据
用一句SQL文怎么实现呢?例如:
select * from (select name from sysobjects where id='123456')当然上面这个例子不能实现
我就是想要这种样式的不知说明白了没有
高人指点。
select @name=name from sysobjects where id='123456'
set @strSQL='select * from '+@name
sp_executesql @strSQL
不过我的意思是说用一句SQL文实现
不能写存储过程的
项目要求的。
不能用sp_execute
就是写一句能直接在分析器里能运行的SQL文
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一句不能
例如
select * from FuncXXXX((select name from sysobjects where id='123456'))
通过那个函数把取出来的对象变成表对象?
快来帮忙啊
着急ing
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非要一句有啥用啊?
把东西都封装起来了
留了填写标准SQL文的处理的功能
所以只能填写标准查询,插入,删除,更新处理语句。
无奈
存储过程也是一个标准的SQL啊,
你那个命令在在哪儿找到的
为什么我在SQL的联机文档里找不到呢?谢谢。
------------
这个是未公开的存储过程,
就是遍历整个数据库的表执行操作,
http://blog.csdn.net/abandonship/archive/2008/06/06/2515943.aspx
Exec sp_helptext sp_MSforeach_worker来查询看具体的内容。
--975342539 --一句
sp_MSforeachtable @command1='select * from ?' , @whereand=' and object_id(o.name)=975342539' 上边两句能否合成一句?谢谢
declare @tablename varchar(20)
declare @sql varchar(1000)
select @tablename=name from sysobjects where id='123456'set @sql ='select * from '+@tablename
exec (@sql)
我考,果然是高手,学习了
还想学习一下
如果不知道那个tb表名
只知道一部分
可不可以用select name from sysobjects where name like '%b' 类似这样的语句替换最后的object_id(''tb'')呢回头给你再加分,谢谢。
@whereand=' and object_id(o.name) in(select object_id(name) from sysobjects where name like ''%b'')'
搞定
这样就可以了sp_MSforeachtable @command1='select * from ?' , @whereand=' and o.name like ''%b'''