我想創建一個腳本來從服務器中的多個數據庫獲取數據,然後我希望它繼續到下一個服務器,並執行相同的操作。我已經創建了一個腳本來處理一臺服務器及其所有數據庫。該腳本對於所有服務器中的所有數據庫表都是相同的。通過服務器迭代SQL Server腳本
我的問題,是我怎麼有腳本識別鏈接服務器,我鏈接到我當前的服務器,並繼續獲得來自這些服務器上的數據?有人可能知道隱藏的存儲過程或命令嗎?
這裏是低於我的劇本,我希望有腳本經過5個,我目前已經鏈接到我的服務器中的一個服務器上運行:
SET NOCOUNT ON
DECLARE @DBNAME NVARCHAR(MAX)
DECLARE @DBNAME1 NVARCHAR(MAX)
DECLARE @varSQL NVARCHAR(MAX)
DROP TABLE TEMPIMGCOUNTERSERVER3
CREATE TABLE TEMPIMGCOUNTERSERVER3
(DBname NVARCHAR(MAX),
Images INT,
FileSize INT,
DBCreation DATETIME)
DECLARE DBNAME CURSOR FAST_FORWARD FOR
select name from sys.databases where [NAME] LIKE 'Z%' AND create_date between '2011-02-06' and '2011-02-12'
ORDER BY [create_date]
OPEN DBNAME
FETCH NEXT FROM DBNAME INTO @DBname
WHILE (@@FETCH_STATUS=0)
BEGIN
Set @varSQL='INSERT INTO TEMPIMGCOUNTERSERVER3 (DBNAME, IMAGES, FileSize, DBCreation)
SELECT ''['[email protected]+']'', SUM(PGCOUNT), sum(filesize/1024/1024), sys.databases.CREATE_DATE
FROM SYS.DATABASES, ['[email protected]+'].dbo.tbldoc WHERE created between ''2011-02-06'' and ''2011-02-12''
and sys.databases.name='''[email protected]+'''
GROUP BY sys.databases.NAME, sys.databases.CREATE_DATE'
EXEC SP_EXECUTESQL @varSQL
FETCH NEXT FROM DBNAME
INTO @DBNAME
END
CLOSE DBNAME
DEALLOCATE DBNAME
INSERT TEMPIMGCOUNTERSERVER3 (DBNAME, IMAGES, FILESIZE)
SELECT @@SERVERNAME + ' ' +'TOTAL IMAGES AND FILE SIZE', SUM(IMAGES), SUM(FILESIZE) FROM TEMPIMGCOUNTERSERVER3
SELECT DBNAME, IMAGES, FILESIZE, convert(VARCHAR(10),
DBCREATION, 101) AS DBCREATION FROM TEMPIMGCOUNTERSERVER3
GROUP BY DBNAME, IMAGES, FILESIZE, convert(VARCHAR(10), DBCREATION, 101)
HAVING SUM(IMAGES) is not null
ORDER BY DBCREATION