zoukankan      html  css  js  c++  java
  • TSQL从备份数据库表拷贝数据到当前数据库表

    在开发过程中定期备份数据库是一个好习惯。今天在更新本地开发数据库的一张配置表时不小心将全表更新了(选择执行SQL语句时漏掉了后面的where语句)。由于开发过程中至少每隔一个月有一次数据库备份,所以可以很容易的使用备份数据库恢复(并覆盖)数据。不过从上次备份到现在,当前数据库里已经保存了一些新的数据,这些新的数据还想继续使用,所以就不能直接采用整个数据库都覆盖的恢复方式。针对本次这个情况,只需要恢复配置表即可,于是有下面的恢复数据语句。

    declare @v1 int
    declare @v2 int
    declare @v3 int
    declare @v4 int
    declare @v5 nvarchar(max)
    
    declare @v10 int
    declare @v20 int
    declare @v30 int
    declare @v40 int
    declare @v50 nvarchar(max)
    
    declare @index int
    declare @changeCount int
    set @index=1
    set @changeCount=0
    
    declare @maxIndex int
    select @maxIndex=MAX(SettingValueID) from bkp_db.dbo.ApplicationSettingValue
    print @maxIndex
    if(@maxIndex<(select MAX(SettingValueID) from current_db.dbo.ApplicationSettingValue))
    begin
      select @maxIndex=MAX(SettingValueID) from current_db.dbo.ApplicationSettingValue
    end
    print @maxIndex
    
    while(@index<@maxIndex)
    begin
      select @v1=ISNULL(F1,0),@v2=ISNULL(F2,0),@v3=ISNULL(F3,0),@v4=ISNULL(F4,0),@v5=ISNULL([Value],'') from bkp_db.dbo.ApplicationSettingValue where SettingValueID=@index
      select @v10=ISNULL(F1,0),@v20=ISNULL(F2,0),@v30=ISNULL(F3,0),@v40=ISNULL(F4,0),@v50=ISNULL([Value],'') from current_db.dbo.ApplicationSettingValue where SettingValueID=@index
      if(@v1=@v10 and @v2=@v20 and @v3=@v30 and @v4=@v40 and @v5<>@v50)
      begin
        update current_db.dbo.ApplicationSettingValue set [Value]=@v5 where SettingValueID=@index
        set @changeCount=@changeCount+1
      end
      set @index=@index+1
    end
    
    print @changeCount
  • 相关阅读:
    iOS-iOS8模拟器设置中文键盘
    iOS-应用性能调优的25个建议和技巧
    IOS-通讯录
    IOS-录音
    IOS-视频
    IOS-音乐
    IOS-音效
    iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
    Android 带你玩转实现游戏2048 其实2048只是个普通的控件(转)
    Android TagFlowLayout完全解析 一款针对Tag的布局(转)
  • 原文地址:https://www.cnblogs.com/josephchan/p/sqlserver_backup_copydata.html
Copyright © 2011-2022 走看看