無法直接備份還原資料庫到新主機,所以一直在測試複製數據的方式,
一開始很笨的每次都將資料庫整個刪掉再透過語法重新建立表格,
因為懶的慢慢測試,後來還是google到了方法~
DECLARE @TableName VARCHAR(MAX) DECLARE @DBName VARCHAR(MAX)='Spec' IF (select OBJECT_ID('tempdb..#BASETABLE_TMP')) IS NOT NULL DROP TABLE #BASETABLE_TMP select TABLE_NAME into #BASETABLE_TMP from INFORMATION_SCHEMA.TABLES--從系統抓取表格名稱 where TABLE_TYPE='BASE TABLE' and TABLE_CATALOG=@DBName DECLARE CUR_TMP CURSOR FOR (SELECT TABLE_NAME FROM #BASETABLE_TMP) OPEN CUR_TMP FETCH NEXT FROM CUR_TMP INTO @TableName WHILE @@FETCH_STATUS=0 BEGIN --表格存在才動作 IF (select OBJECT_ID(@TableName)) IS NULL BEGIN FETCH NEXT FROM CUR_TMP INTO @TableName CONTINUE END --顯示表格名稱 PRINT @TableName --TUNCATE 較 DELETE 快速,不會有交易紀錄檔 EXEC('TRUNCATE TABLE '+@TableName) --將資料跨伺服器寫入 EXEC('INSERT INTO '+@TableName+' select * from [連結伺服器].'+@DBName+'.dbo.'+@TableName) FETCH NEXT FROM CUR_TMP INTO @TableName END CLOSE CUR_TMP DEALLOCATE CUR_TMP
參考自
The Will Will Will Web-SQL Tips: 刪除資料庫中所有資料表的資料
沒有留言:
張貼留言