数据库备份通用脚本
1.创建一个新库;
2.从旧库中导出生成表,视图,存储过程;(GpsExpiredData,GpsReceRealTime两张表不还原)
3.在新创建库中输入创建脚本;
4.数据迁移;
(迁移时注意:GpsExpiredData,GpsReceRealTime,GpsReceRealTime_buff1,GpsReceRealTime_buff2,
GpsOutEquipmentHistory_buff,GpsOutEquipmentHistory,report_开头的;不要导出数据);
5.新数据库备份\分离\脱机----->copy
/****** 对象: StoredProcedure [dbo].[Proc_InsertData] 脚本日期: 12/13/2011 10:46:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Proc_InsertData](
@tbname varchar(50) ----目标数据库
,@sourcetbname varchar(200)) ----源数据库
AS
BEGIN
declare @Str varchar(max) , @sql varchar(max)
SELECT @Str=REPLACE(REPLACE(
(SELECT c.name
FROM sys.columns c JOIN sys.objects o ON (c.object_id = o.object_id)
WHERE o.name=@tbname FOR XML PATH('')
),'<name>','['),'</name>','],')
set @Str= LEFT(@str,LEN(@str)-1) ---获取表字段字符串
declare @mark int
Select @mark=OBJECTPROPERTY(OBJECT_ID(@tbname),'TableHasIdentity') ---判断是否含有标志列
if @mark = 1
set @sql= --'TRUNCATE TABLE '+ @tbname
' SET IDENTITY_INSERT '+ @tbname + ' ON '
+' INSERT '+@tbname+'('+@Str+') SELECT '+@Str +' FROM '+ @sourcetbname
+' SET IDENTITY_INSERT '+ @tbname + ' OFF '
else
set @sql = --'TRUNCATE TABLE '+ @tbname
' INSERT '+@tbname+'('+@Str+') SELECT '+@Str +' FROM '+ @sourcetbname
print @sql
exec(@sql) ----执行插入数据操作
END
GO
-------------------------------------------------------------------------
/****** 对象: StoredProcedure [dbo].[Proc_ImportDataFromDB] 脚本日期: 12/13/2011 10:45:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--在目标数据库中执行
CREATE PROCEDURE [dbo].[Proc_ImportDataFromDB] @DBName VARCHAR(40) --源数据库名称
AS
BEGIN
DECLARE @tbname VARCHAR(40) ,@sql VARCHAR(max)
DECLARE t_cursor CURSOR FOR
SELECT name FROM sysobjects WHERE TYPE = 'U ' ORDER BY name
OPEN t_cursor
FETCH NEXT FROM t_cursor INTO @tbname
WHILE @@FETCH_STATUS=0
BEGIN
IF (@tbname NOT IN ('Elec_CurrentPosInfo'
,'')) -- 相关轨迹数据或临时数据表,不进行导入
BEGIN
SET @sql= ' PROC_InsertData '''+ @tbname + ''' , '''+@DBName+'.dbo.' + @tbname +''''
PRINT @sql
EXEC(@sql)
END
FETCH NEXT FROM t_cursor INTO @tbname
END
CLOSE t_cursor
DEALLOCATE t_cursor
END
-----------------------------
exec [Proc_ImportDataFromDB] '--DB'