-1

SQL SELECT 语句如何从服务器上的所有数据库中返回一行,但仅从特定表和表的某些列中返回。

或者,在伪代码中,是这样的:

for each(database)
{
  return database.column.row;
} 
4

1 回答 1

0

这没有经过测试,但它应该足以让你开始。

DECLARE @name as NVARCHAR(128);
DECLARE @sql AS NVARCHAR(max);

DECLARE c_Cursor CURSOR FOR
SELECT databases.name
  FROM sys.databases
 WHERE databases.database_id > 4
 ORDER BY databases.name;

OPEN c_Cursor;

FETCH NEXT FROM c_Cursor
 INTO @name;

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @sql = N'SELECT table.* FROM ' + QUOTENAME(@name) + N'.dbo.table';

    EXEC @sql

    FETCH NEXT FROM c_Cursor
     INTO @name;

END;

CLOSE c_Cursor;
DEALLOCATE c_Cursor;
于 2013-06-14T16:21:15.277 回答