0
我正在編寫數據遷移的存儲過程。在那裏,我需要將表和其他數據庫的整個數據複製到當前數據庫的臨時表中。但是,數據庫名稱將作爲參數。然後我需要處理複製的數據。但是,我不知道如何使用動態數據庫名稱。複製Microsoft SQL Server中不同數據庫的數據表
CREATE PROCEDURE [dbo].[MGRT]
@DBName VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#TMPCH') IS NOT NULL
DROP TABLE #TMPCH;
SELECT * INTO #TMPCH FROM MDDX.dbo.CH ORDER BY MDate,Mtime
DECLARE @count INT = 0
DECLARE @curRow INT = 0
SELECT @count = COUNT(*) FROM #TMPCH
WHILE @curRow <= @count
BEGIN
DECLARE @lastDate NVARCHAR(10) = '-'
SET @lastDate = (SELECT TOP 1 To_Date FROM CH ORDER BY SysKey DESC)
IF @lastDate = '' OR @lastDate IS NULL
BEGIN
SET @lastDate = '-'
PRINT @lastDate
END
INSERT INTO CH(syskey,CurCode,CurDesc,CurSymbol,CurRate,From_Date,To_Date,userid,username)
(SELECT @curRow,CurCode,CurDesc,CurSymbol,CURRATE,@lastDate,MDate,'admin','Administrator'
FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY MDate,MTime) AS rowNo FROM #TMPCH) AS TMP WHERE rowNo = @curRow)
SET @curRow = @curRow + 1
END
END
我需要使用@DBName而不是MDDX來獲取數據。
謝謝!
您可以使用動態查詢。顯示你到目前爲止嘗試過的。 – Rahul 2014-11-04 07:56:13
這只是示例。實際上,我需要更多的計算。謝謝你! – 2014-11-04 08:06:30