SQL SELECT语句如何从服务器上的所有数据库中返回一行,而只是从特定的表和表的某些列返回。
或者,在伪代码中,像这样:
for each(database)
{
return database.column.row;
}
答案 0 :(得分: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;