日期:2014-05-18  浏览次数:20462 次

求助,大量数据更新update的存储过程(procedure)

st_NO         FLG
------     -----
00001           1
00002           0
00003           0
00004           0
大约200万件左右,已经把符合更新条件的st_NO放进char*   BUFFER_ST_NO[5]中(里面有00002和00003),
请帮忙写一个存储器或触发器之类的东西,把BUFFER_ST_NO传进去,就可以把相应的FLG更新成 '1 '.
st_NO         FLG
------     -----
00001           1
00002           1
00003           1
00004           0
数据量很大,请尽量提高效率,谢谢。


------解决方案--------------------
SQL Server中没有数组的.传数组好像不行.
你可以在程序中先把数组中的内容拼成用特定字符分隔的字符串再传到SQL Serever中处理.
比如:str1= '00002,00003 ' str2= 'BBBB,BBBB '

--创建测试数据
create table aa(st_NO varchar(10),NAME varchar(10), FLG bit)
insert into aa select '00001 ', 'AAAA ',1
union all select '00002 ', 'BBBB ',0
union all select '00003 ', 'BBBB ',0
union all select '00004 ', 'CCCC ',0

--下面的的存储过程.要确保两个传入参数的分隔符 ", "个数一致.
create procedure proc_update(
@str1 varchar(100),
@str2 varchar(100))
AS
Begin
--对参数进行一些判断,如果能确保传入参数符合要求就省了.
create table #temp(No varchar(10),Name varchar(10))

While Charindex( ', ',@str1)> 0
Begin
insert into #temp
select No=Left(@str1,Charindex( ', ',@str1)-1),
Name=Left(@str2,Charindex( ', ',@str2)-1)
select @str1 = stuff(@str1,1,Charindex( ', ',@str1), ' '),
@str2 = stuff(@str2,1,Charindex( ', ',@str2), ' ')
End
insert into #temp select No=@str1,Name=@str2

update a
set
FLG=1
from aa a,#temp b
where a.st_NO=b.No and a.NAME = b.Name
End

--调用测试
select * from aa --显示1
exec proc_update '00002,00003 ', 'BBBB,BBBB '
select * from aa --显示2

/*显示1 结果
00001 AAAA 1
00002 BBBB 0
00003 BBBB 0
00004 CCCC 0
*/

/*显示2 结果
00001 AAAA 1
00002 BBBB 1
00003 BBBB 1
00004 CCCC 0
*/