Wednesday, June 17, 2015

真服了老印

http://www.mitbbs.com/article_t/Database/31187665.html

发信人: iMaJia (iMac,iPod,iPad,i�甲), 信区: Database
标  题: 真服了老印
发信站: BBS 未名空间站 (Wed Jun 17 14:29:59 2015, 美东)

写 Stored Procedure 批量处理数据

1.数数有多少记录需要被处理
select @n = count(*) from tableA where condition = xyz

2.用个functionGetNextRecord, 每次返回一个 ID
select top 1 ID from tableA where condition = xyz order by ID

3.写 WHILE loop
SET @count = 1
SET @NextRecord = functionGetNextRecord()
WHILE (@NextRecord IS NOT NULL) and (@count<=@n) BEGIN
blah blah blah
END


如果我写:
select ID
INTO #temptable
from tableA where condition = xyz

然后用#temptable做工就可以, 一次读表可以做到的硬是作成成千千万万次的读


另外不知道是不是老印的创作. 查 account balance, 没有一个 current balance 的
记录, 现有代码每次都做要 select sum(abc) from TableB with (nolock) where
accountNumber = 123 就算了. 做会计的需要精度, 还nolock... 也不怕 dirty read.
..


No comments:

Post a Comment