复制数据库中的所有表并添加新数据,而不会影响旧数据SQL

时间:2016-10-19 11:33:05

标签: sql sql-server database sql-server-2014

任何人都可以帮我处理一个存储过程,该存储过程将允许复制数据库中的所有表,并且必须在最后截断原始表,并且当该过程每天运行时,必须从原始表复制所有新数据到备份表而不影响现有数据。

这就是我所做的

CREATE PROCEDURE ZS_Backup
AS
BEGIN

SELECT * INTO
 [dbo].[CS_ArrivalsBackUp], 
 [dbo].[CS_awbBackUp],
 [dbo].[CS_awb_arrivalsBackUp],
 [dbo].[CS_awb_dlvBackUp],
 [dbo].[CS_awb_iataBackUp],
 [dbo].[CS_awb_iata_arrivalsBackUp],
 [dbo].[CS_ConsolidationBackUp],
 [dbo].[CS_part_agents],
 [dbo].[CS_part_consignee],
 [dbo].[Evo_Customers],
 [dbo].[Evo_Products],
 [dbo].[int_log_arrivals],
 [dbo].[int_log_deliveries],
 [dbo].[integration_logs],
 [dbo].[period],
 [dbo].[public_holidays],
 [dbo].[rates],
 [dbo].[temp_Rates]

FROM [dbo].[CS_Arrivals],
       [dbo].[CS_awb],
      [dbo].[CS_awb_arrivals],
      [dbo].[CS_awb_dlv],
      [dbo].[CS_awb_iata],
      [dbo].[CS_awb_iata_arrivals],
      [dbo].[CS_Consolidation],
      [dbo].[CS_part_agents],
      [dbo].[CS_part_consignee],
      [dbo].[Evo_Customers],
      [dbo].[Evo_Products],
      [dbo].[int_log_arrivals],
      [dbo].[int_log_deliveries],
      [dbo].[integration_logs],
      [dbo].[period],
      [dbo].[public_holidays],
      [dbo].[rates],
      [dbo].[temp_Rates]

TRUNCATE TABLE [dbo].[CS_Arrivals],
           [dbo].[CS_awb],
           [dbo].[CS_awb_arrivals],
           [dbo].[CS_awb_dlv],
           [dbo].[CS_awb_iata],
           [dbo].[CS_awb_iata_arrivals],
           [dbo].[CS_Consolidation],
           [dbo].[CS_part_agents],
           [dbo].[CS_part_consignee],
           [dbo].[Evo_Customers],
           [dbo].[Evo_Products],
           [dbo].[int_log_arrivals],
           [dbo].[int_log_deliveries],
           [dbo].[integration_logs],
           [dbo].[period],
           [dbo].[public_holidays],
           [dbo].[rates],
           [dbo].[temp_Rates]

           END
           GO

2 个答案:

答案 0 :(得分:1)

你走了,

小心你跑的地方。为了您的安全,我已经注释掉了实际执行复制和截断的位,但我设置了它以便它会显示将要执行的内容:

DECLARE @table_name nvarchar(1000)
DECLARE @SQL nvarchar(1000)

DECLARE CUR CURSOR 
FOR 
SELECT name 
FROM sys.tables
WHERE type = 'U'
and name IN ('CS_ArrivalsBackUp', 
'CS_awbBackUp',
'CS_awb_arrivalsBackUp',
'CS_awb_dlvBackUp',
'CS_awb_iataBackUp',
'CS_awb_iata_arrivalsBackUp',
'CS_ConsolidationBackUp',
'CS_part_agents',
'CS_part_consignee',
'Evo_Customers',
'Evo_Products',
'int_log_arrivals',
'int_log_deliveries',
'integration_logs',
'period',
'public_holidays',
'rates',
'temp_Rates')
ORDER BY NAME

OPEN CUR

FETCH NEXT FROM CUR
INTO @table_name

WHILE @@FETCH_STATUS = 0  
BEGIN  

PRINT 'SELECT * INTO TMP_' + @table_name + ' FROM ' + @table_name
PRINT 'TRUNCATE TABLE ' + @table_name 

--SET @SQL = 'SELECT * INTO TMP_' + @table_name + ' FROM ' + @table_name
--EXEC (@SQL)

--SET @SQL = 'TRUNCATE TABLE ' + @table_name 
--EXEC (@SQL)

FETCH NEXT FROM CUR INTO @table_name  

END
CLOSE CUR
DEALLOCATE CUR

答案 1 :(得分:1)

好的只需要稍作改动。创建临时表后(第一次运行上面);然后为将来的运行你需要:

DECLARE @table_name nvarchar(1000)
DECLARE @SQL nvarchar(1000)

DECLARE CUR CURSOR 
FOR 
SELECT name 
FROM sys.tables
WHERE type = 'U'
and name IN ('CS_ArrivalsBackUp', 
'CS_awbBackUp',
'CS_awb_arrivalsBackUp',
'CS_awb_dlvBackUp',
'CS_awb_iataBackUp',
'CS_awb_iata_arrivalsBackUp',
'CS_ConsolidationBackUp',
'CS_part_agents',
'CS_part_consignee',
'Evo_Customers',
'Evo_Products',
'int_log_arrivals',
'int_log_deliveries',
'integration_logs',
'period',
'public_holidays',
'rates',
'temp_Rates')
ORDER BY NAME

OPEN CUR

FETCH NEXT FROM CUR
INTO @table_name

WHILE @@FETCH_STATUS = 0  
BEGIN  

PRINT 'INSERT INTO TMP_' + @table_name + ' SELECT * FROM_' + @table_name
PRINT 'TRUNCATE TABLE ' + @table_name 

--SET @SQL = 'INSERT INTO TMP_' + @table_name + ' SELECT * FROM_' + @table_name
--EXEC (@SQL)

--SET @SQL = 'TRUNCATE TABLE ' + @table_name 
--EXEC (@SQL)

FETCH NEXT FROM CUR INTO @table_name  

END
CLOSE CUR
DEALLOCATE CUR