我有以下查询。有人可以帮助在光标中敲击它。
IF OBJECT_ID('tempdb.[dbo].[#Results]') IS NOT NULL
DROP TABLE [dbo].[#Results]
GO
CREATE TABLE [dbo].[#Results] (
[DatabaseName] VARCHAR(128) NULL,
[DatabaseVersion] VARCHAR(128) NULL,
[DateChangedOn] DATETIME NULL)
EXEC sp_msForEachDb '
IF EXISTS (SELECT * FROM [?].sys.objects WHERE NAME = ''stdVersions'')
BEGIN
INSERT INTO #Results
SELECT
''?'' AS [DatabaseName],
v.[DatabaseVersion],
l.[DateChangedOn]
FROM [?].dbo.stdVersions v
CROSS JOIN [?].dbo.stdChangeLog l
END'
SELECT * FROM #Results ORDER BY DateChangedOn
查询应返回服务器中所有数据库名称的列表,并在数据库中返回databaseversion列和DateChangeOn列。服务器中的所有数据库都包含名为stdVersions和stdChangeLog的表,其中stdVersion表具有单行DatabaseVersion,而stdChangeLog表具有单行DateChangedOn。
答案 0 :(得分:0)
下面是在#Results表上创建游标的示例。
DECLARE @DatabaseName sysname
, @DatabaseVersion sysname
, @DateChangedOn datetime;
DECLARE DatabaseList CURSOR LOCAL FAST_FORWARD
FOR
SELECT DatabaseName
, DatabaseVersion
, DateChangedOn
FROM #Results
ORDER BY DateChangedOn;
OPEN DatabaseList;
WHILE 1 = 1
BEGIN
FETCH NEXT FROM DatabaseList INTO
@DatabaseName
, @DatabaseVersion
, @DateChangedOn;
IF @@FETCH_STATUS = -1 BREAK;
--process row here
END;
CLOSE DatabaseList;
DEALLOCATE DatabaseList;
你提到" std"表存在于所有数据库中。如果这些表不包含系统数据库,请在下面的脚本中更改IF语句以排除这些:
IF EXISTS (SELECT * FROM [?].sys.objects WHERE NAME = ''stdVersions'')
AND [?] NOT IN(N'master', N'model', N'tempdb', N'msdb', N'SSISDB')