T-SQL从备份数据库表拷贝数据到当前数据库表

在开发过程中定期备份数据库是一个好习惯。今天在更新本地开发数据库的一张配置表时不小心将全表更新了(选择执行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

转载于:https://www.cnblogs.com/josephchan/archive/2012/11/20/sqlserver_backup_copydata.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值