sql server 如何批量update数据
2016-02-12 16:22
447 查看
原因:
要一次性update 2千万条数据,虽然update sql很简单,但是由于一次性修改太多的数据,造成数据库log满了,就会报error:
[ErrorCode: 9002, SQL State: S0004] The transaction log for database'XXXXData' is full. To find out why space in the log cannot be reused, seethe log_reuse_wait_desc column in sys.databases
所以:
因为是数据库一次性update 数据操作,因此,就想到批量的update数据。 我能想到的解决方案就是,像分页查询 sql那样,每次update 一定数量的数据(事务提交)。
那么,我的update script是如下实现的:
如果,各位大师,有别的,好的,简单的解决方案,请分享一下~~~~~
要一次性update 2千万条数据,虽然update sql很简单,但是由于一次性修改太多的数据,造成数据库log满了,就会报error:
[ErrorCode: 9002, SQL State: S0004] The transaction log for database'XXXXData' is full. To find out why space in the log cannot be reused, seethe log_reuse_wait_desc column in sys.databases
所以:
因为是数据库一次性update 数据操作,因此,就想到批量的update数据。 我能想到的解决方案就是,像分页查询 sql那样,每次update 一定数量的数据(事务提交)。
那么,我的update script是如下实现的:
DECLARE @pagesize INT, @pages INT, @offset INT, @maxresult INT select @pagesize=100000, @pages=1 -- 1 prepare data list CREATE TABLE #Tbl_Affected_ID ( id INT NOT NULL, rowNum INT NOT NULL, modified CHAR(1) DEFAULT 'N' NOT NULL ) INSERT #Tbl_Affected_ID(id, rowNum) SELECT aa.id, ROW_NUMBER() OVER(ORDER BY aa.id) AS rowNum FROM TBLxxx aa WHERE userId is null '?' -- 2 batch update WHILE exists( select 1 from #Tbl_Affected_ID where modified = 'N') BEGIN select @offset = (@pages-1) * @pagesize select @maxresult = @offset + @pagesize begin tran UPDATE TBLxxx SET userId = 'test_user' from #Tbl_Affected_ID tmp WHERE tmp.id = TBLxxx.id and rowNum between @offset and @maxresult UPDATE #Tbl_Affected_ID SET modified = 'Y' WHERE rowNum between @offset and @maxresult commit tran select @pages = @pages+1 END
如果,各位大师,有别的,好的,简单的解决方案,请分享一下~~~~~
相关文章推荐
- 得到刚刚插入数据库数据的主键ID
- Oracle varchar2 length 分析
- MySQL—基本操作
- JUnit Test NOSQL
- mongodb最新java驱动CRUD使用演示
- SQLServer 常用工具类SQL
- XML VS SQLite
- mongodb分片部署
- mongodb副本集
- 通过注册表查询Visual C++ Redistributable Package是否安装
- 轻松解决MYSQL数据库连接过多的错误
- EXCEL数据导入数据库表
- 学习MongoDB(Troubleshoot Replica Sets) 集群排除故障
- MySQL 单向同步复制
- T-SQL 公用表表达式(CTE)
- SQL 操作结果集 -并集、差集、交集、结果集排序
- SQL语句 - 嵌套查询
- 疑难杂症 - SQL语句整理
- SQL查询 - 表连接
- SQL语句 - 数据操作