日期:2014-05-18  浏览次数:20588 次

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 '+@dbname+ ' from '+@dbname+ '.dbo.sysobjects where xtype= ' 'U ' ' '
fetch next from cur into @dbname
end
close cur
deallocate cur
print @sql