当前位置: 代码迷 >> Sql Server >> Select 有关问题 ?
  详细解决方案

Select 有关问题 ?

热度:102   发布时间:2016-04-27 17:42:58.0
Select 问题 ??
我要从系统表中得出每个数据库中的表

select   *   from   Northwind.dbo.sysobjects   where   xtype= 'U '

这个是   Northwind   库,但我需要所有的,这样就要   from   不同的库:

Northwind.dbo.sysobjects
master.dbo.sysobjects
testa.dbo.sysobjects       等等……,我可以得到所有库的名字,但我用的方法是:

string   str= "select   *   from "+dbname+ ".dbo.sysobjects   where   xtype= 'U ' "   dbname--> 是数据库名   ,运行时出错,语法错误。   不知道这种情况下应该怎样组合sql   语句   dbname+ ".dbo.sysobjects "
  谢谢,不知道说明白没。

------解决方案--------------------
select Name from sysobjects where xtype= 'u ' and status> =0


查看数据库所有表
------解决方案--------------------


declare @dbname varchar(50)
declare @sql varchar(8000)
set @sql = ' '
declare cur cursor scroll for select name from master..sysdatabases
open cur
fetch next from cur into @dbname
while @@fetch_status = 0
begin
if @sql <> ' '
set @sql = @sql + '; '
set @sql = @sql + 'select name as '[email protected]+ ' from '[email protected]+ '.dbo.sysobjects where xtype= ' 'U ' ' '
fetch next from cur into @dbname
end
close cur
deallocate cur
print @sql


  相关解决方案