设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 809|回复: 0

A simple and safe way to back up your SQL data

[复制链接]
发表于 2011-8-5 15:45:18 | 显示全部楼层 |阅读模式
In many middle or small companies, peogrammers usually have some time to test or change coding in a real system, sometimes its dangerious to do that way, even did you do some menual backup of the real data, and sometimes you can not control that even the data backup itsself still can cause some data losing or strcture destory. so try to use stored procedures or just some script snippets will give you much easy, convenient and safe way to make that for you. actually you can still do some detail coding to make it use more friendly.

an easy script may simmilar like this:


  1.   PRINT 'Clear order data in the mirror database ...'
  2. DELETE FROM [WilkyDB_Mir].[dbo].[His_Order_1]
  3.     --- WHERE (Cat_ID = 2011)
  4. PRINT 'Copying order data to the mirror database ...'
  5. INSERT INTO [WilkyDB_Mir].[dbo].[His_Order_1]
  6.         (RDate, ID, Name, CustomerID, ord_weight, ord_lns, issales_id, ord_num, inv_date, shp_date, ctype, stock_locat)
  7. SELECT     RDate, ID, Name, CustomerID, ord_weight, ord_lns, issales_id, ord_num, inv_date, shp_date, ctype, stock_locat
  8. FROM         WilkyDB.dbo.His_Order_1
  9. ---WHERE     (Cat_ID = 2011)
  10. PRINT 'Data mirror of order completed.'
复制代码

Do you think it's not clear enough for what's the codes doiing? you can always make it better, like:

  1. DECLARE @mCount int, @mDate datetime;
  2. SELECT @mCount=0, @mDate=GetDate();

  3. SELECT @mCount=COUNT(*) FROM [WilkyDB_Mir].[dbo].[Dict];
  4. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB_Mir].[dbo].[Dict]
  5.      ORDER BY DT_Create Desc;
  6. PRINT 'Clear data dictionary data in the mirror database ...'
  7. PRINT 'Total number of '+CONVERT(varchar, @mCount)+' last updated on: '+CONVERT(varchar,@mDate)+'.'
  8. DELETE FROM [WilkyDB_Mir].[dbo].[Dict]

  9. SELECT @mCount=COUNT(*) FROM [WilkyDB].[dbo].[Dict];
  10. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB].[dbo].[Dict]
  11.      ORDER BY DT_Create Desc;
  12. PRINT 'Copying data of '+CONVERT(varchar, @mCount)+' data dictionary to the mirror database ...'
  13. INSERT INTO Dict
  14. SELECT     Label, Cat_ID, KEY_Code, KEY_Name, KEY_LNK, DT_Enable, DT_Disable, DT_Create, DT_Modify, Operator, Status, Notes
  15. FROM         WilkyDB.dbo.Dict AS Dict_1
  16.         
  17. SELECT @mCount=COUNT(*) FROM [WilkyDB_Mir].[dbo].[Dict];
  18. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB_Mir].[dbo].[Dict]
  19.      ORDER BY DT_Create Desc;
  20. PRINT 'Total of '+CONVERT(varchar, @mCount)+' data dictionary numbers are backed up dated to: '+CONVERT(varchar,@mDate)+'.'
  21. PRINT 'Data dictionary mirror completed.'
复制代码
Seems more clear? but how if the database is huge and maybe you just want to backup the part usually changed? There you go:
  1. PRINT 'Clear 2011 BSC Plan data in the mirror database ...'

  2. DELETE FROM [WilkyDB_Mir].[dbo].[BSCSalesPlan]
  3. WHERE (Cat_ID = 2011)

  4. PRINT 'Copying 2011 BSC Plan data to the mirror database ...'

  5. INSERT INTO WilkyDB_Mir.dbo.BSCSalesPlan
  6. (Cat_ID, Bus_Unit, Prov, Branch, SalesPerson, CustID, CustName, OLS, CSR, PA, GV, ProcSCalls, TolSCalls, Tons, Sales, CGS, CostPerTon, GP, GPP,
  7. NumOfOrders, GMI)
  8. SELECT Cat_ID, Bus_Unit, Prov, Branch, SalesPerson, CustID, CustName, OLS, CSR, PA, GV, ProcSCalls, TolSCalls, Tons, Sales, CGS, CostPerTon, GP, GPP,
  9. NumOfOrders, GMI
  10. FROM WilkyDB.dbo.BSCSalesPlan
  11. WHERE (Cat_ID = 2011)

  12. PRINT 'Data mirror of 2011 BSC Plan completed.'
复制代码
You see, just a little bit further consideration, you can have it easily.

Then, mayb eit's a good idea to put them into the stored procedures. so you no need to worry about where to find it out. just expand the MSSMS, find the SP name, then click it. Done!

  1. USE [WilkyDB_Mir]
  2. GO

  3. /****** Object: StoredProcedure [dbo].[Back_Dict] Script Date: 08/05/2011 16:34:15 ******/
  4. SET ANSI_NULLS ON
  5. GO

  6. SET QUOTED_IDENTIFIER ON
  7. GO



  8. -- =============================================
  9. -- Author: John Zhang (cq_box @ 126.com)
  10. -- Create date: August 5, 2011
  11. -- Description: Backup data dictionary before any updating.
  12. -- =============================================
  13. CREATE PROCEDURE [dbo].[Back_Dict]
  14. AS
  15. BEGIN
  16. -- SET NOCOUNT ON added to prevent extra result sets from
  17. -- interfering with SELECT statements.
  18. SET NOCOUNT ON;

  19. DECLARE @mCount int, @mDate datetime;
  20. SELECT @mCount=0, @mDate=GetDate();

  21. SELECT @mCount=COUNT(*) FROM [WilkyDB_Mir].[dbo].[Dict];
  22. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB_Mir].[dbo].[Dict]
  23. ORDER BY DT_Create Desc;
  24. PRINT 'Clear data dictionary data in the mirror database ...'
  25. PRINT 'Total number of '+CONVERT(varchar, @mCount)+' last updated on: '+CONVERT(varchar,@mDate)+'.'

  26. DELETE FROM [WilkyDB_Mir].[dbo].[Dict]
  27. --- WHERE (Cat_ID = 2011)

  28. SELECT @mCount=COUNT(*) FROM [WilkyDB].[dbo].[Dict];
  29. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB].[dbo].[Dict]
  30. ORDER BY DT_Create Desc;

  31. PRINT 'Copying data of '+CONVERT(varchar, @mCount)+' data dictionary to the mirror database ...'

  32. INSERT INTO Dict
  33. SELECT Label, Cat_ID, KEY_Code, KEY_Name, KEY_LNK, DT_Enable, DT_Disable, DT_Create, DT_Modify, Operator, Status, Notes
  34. FROM WilkyDB.dbo.Dict AS Dict_1
  35. ---WHERE (Cat_ID = 2011)

  36. SELECT @mCount=COUNT(*) FROM [WilkyDB_Mir].[dbo].[Dict];
  37. SELECT TOP 1 @mDate=DT_Create FROM [WilkyDB_Mir].[dbo].[Dict]
  38. ORDER BY DT_Create Desc;
  39. PRINT 'Total of '+CONVERT(varchar, @mCount)+' data dictionary numbers are backed up dated to: '+CONVERT(varchar,@mDate)+'.'

  40. PRINT 'Data dictionary mirror completed.'

  41. END

  42. GO
复制代码
This is the original article, if you think its useful please let me know by just drop some words here. Thanks!





您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-25 23:45 , Processed in 0.014472 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表