写个存储过程,紧急紧急,帮朋友问个问题,写个存儲過程,会者不难,建议者有分.(200分)

  • 主题发起人 主题发起人 bubble
  • 开始时间 开始时间
B

bubble

Unregistered / Unconfirmed
GUEST, unregistred user!
現在我用delphi在開發程序。我在前台處理數據的時候,數據量大啦!就很慢。要花好長時間。如何提高數據處理能力?我現在是改為後台sql server來處理。
我遇到一個問題。我現在有兩個表。table1 與 table2
table1裡有 aa varchar(20),bb varchar(20),cc varchar(20),dd float(8) 等這幾個字段
table2裡有 tt varchar(20),ee varchar(20),mm varchar(20), nn float(8) 等這幾字段。

現在我的table1裡有50000條記錄。
table2裡有1000000條記錄。我要將table1裡的50000條記錄一一的讀書出
在tabel2裡進行查找,如查找到啦。就將table2裡的 nn這個字段進行updata nn=nn+table1.dd
如果沒有找到就將table1裡的這條記錄插入到table2裡。
我想用存儲過程來實現。請問如何解決呢?

谢谢大家,主要是提高效率和写存储过程。

用Query行不行??呵呵。
 
if exist(select * from table1 a,table2 b where a.aa=b.tt and a.bb=b.ee and a.cc=b.mm)
update table2 a,table1 b
set
a.nn=a.nn+b.nn
where a.aa=b.tt and a.bb=b.ee and a.cc=b.mm
else
insert into table2 select * from table1

先更新相同的,删除相同的,然后再插入
 
建议你在两个表里都建主键(int)如果两个表有这么多字段相同,
其中一个就保留主键就行了,
另外没必要保留这么多数据,用一定的条件分开它
例如建一个叫T200204的表保存4月份的数据就行了。
 
create procedure AProcedure
AS

/* 声明变量*/
declare @aa varchar(20),@bb varchar(20),@cc varchar(20), @dd varchar(20)

/* 定义cursor */

/* 先处理 table2 中没有的记录 ,将TABLE1中的记录补充到TABLE2 */
declare ins_curs cursor for
select aa,bb,cc,dd from table1 a,table2 b
where (a.aa <> b.tt) and (a.bb<>b.ee) and (a.cc<>b.mm)

open ins_curs
fetch next from ins_curs into @aa,@bb,@cc,@dd
while (@@fetch_status<> -1 )
begin
if ( @@fetch_status<>-2 )
insert into table2 (tt,ee,mm,nn) values(@aa,@bb,@cc,@dd)
fetch next from ins_curs into @aa,@bb,@cc,@dd
end
close ins_curs
deallocate ins_curs


/* 声明CURSOR 查找出相同记录,然后逐条更新TABLE2 */
deallocate upd_curs

declare upd_curs cursor for
select aa,bb,cc,dd from table1 a,table2 b
where (a.aa = b.tt) and (a.bb=b.ee) and (a.cc=b.mm)

open upd_curs
fetch next from upd_curs into @aa,@bb,@cc,@dd
while (@@fetch_status<> -1 )
begin
if ( @@fetch_status<>-2 )
update table2 set nn=nn+@dd where aa=@aa and bb=@bb and cc=@cc
fetch next from upd_curs into @aa,@bb,@cc,@dd
end
close upd_curs
deallocate upd_curs

go
 
你可以根据他们的外键创建视图,这个视图的数据就是table1能在table2找到的数据

视图应该包括nn,dd字段,然后Update 视图 set nn=nn+dd
 
谢谢各位大虾,结!^_^
 
后退
顶部