触发器过程优化

hugeant 2002-02-06 02:10:03

4张 300个字段的表table1,table2,table3,table4 字段名一样,前2张表中只都有
一条记录, 现在要做如下处理
Declare @ItemCount
select @ItemCount = ....
/*取差值*/
table3.item1 = table1.item1 - table2.item1
。。。。
。。。。
table3.item300 = table1.item300 - table2.item300
/*取平均*/
table4.item1 = (table1.item1 + table2.item1)/@ItemCount
。。。。
。。。。
table4.item300 = (table1.item300 + table2.item300)/@ItemCount
有什么快速的优化的执行方法?
我现在也是用exec函数 做循环,
declare sqltext nvarchar(256)

for i=1 to 300 begin
/*先运算差值*/
/*取出第一个表的字段值*/
delete temptable
select sqltext='insert temptable(Value) Select item'+convert(varchar,i)+
' from table1 '+
' where rDate='''+convert(varchar(20),@CurrentDate)+''''
select @tempFloat1 = value from temptable

/*取出第二个表的字段值(方法同上)*/
....
/*做运算*/
select @tempFloat1 = @tempFloat1-@tempFloat2
/*做insert 到table3*/


/*再用同样的办法计算平均值*/

end

但是2个循环下来(一次差值, 一次平均),花了14~19秒的时间,而现在我需要在2秒内就运算完毕(该过程在触 发器中使用) 各位有什么好招吗?
各位过路的神仙,路过的大侠,看一眼吧,伸伸手吧 :)
...全文
42 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hypmonkey 2002-02-21
  • 打赏
  • 举报
回复
发过可以看到自己的帖子啊
hypmonkey 2002-02-21
  • 打赏
  • 举报
回复
kankan
昵称被占用了 2002-02-07
  • 打赏
  • 举报
回复
呵呵,rwq_(风云浪子) 的方法能解决字段太多的问题!
hugeant 2002-02-07
  • 打赏
  • 举报
回复
这个主意不错,我回去试试
还是这里高手多啊,我在大富翁上问了半天,都是答非所问
rwq_ 2002-02-07
  • 打赏
  • 举报
回复
接上面的:
execute(sqlstr)--求差值!
rwq_ 2002-02-07
  • 打赏
  • 举报
回复
我把你和海阔天空的结合起来:
sqlstr=''insert into table3 select ''
for i=1 to 299
begin
sqlstr=sqlstr+'' t1.item''+convert(varchar(5),i)+''-t2.item''+convert(varchar(5),i)+'',''
end

sqlstr=sqlstr+'' t1.item300-t2.item300 from table1 ta1,table2 t2''




另一个一样的道理…!


progame 2002-02-06
  • 打赏
  • 举报
回复
是吗
昵称被占用了 2002-02-06
  • 打赏
  • 举报
回复
发现问题!
回复后看不到自己的回复,按“后退”才看得到!

呵呵,比较有趣的BUG!
昵称被占用了 2002-02-06
  • 打赏
  • 举报
回复
呵呵,新版感觉还可以!

有点不习惯!
昵称被占用了 2002-02-06
  • 打赏
  • 举报
回复
是不是可以两个语句搞定:
insert Table3
select table1.item1 - table2.item1 as item1,
table1.item2 - table2.item2 as item2,
...
table1.item300 - table2.item300 as item300
from table1,table2

insert Table4
select case when table1.item1 = table2.item1 then 0 else (table1.item1 + table2.item1)/(table1.item1 - table2.item1) end as item1,
case when table1.item2 = table2.item2 then 0 else (table1.item2 + table2.item2)/(table1.item2 - table2.item2) end as item2,
...
case when table1.item300 = table2.item300 then 0 else (table1.item300 + table2.item300)/(table1.item300 - table2.item300) end as item300
from table1,table2

不知道贴主为何搞得那么复杂!
hugeant 2002-02-06
  • 打赏
  • 举报
回复
上面的for 循环中有点小错误,忘了加上exec(sqltext)
不过就是那个意思

11,850

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 非技术版
社区管理员
  • 非技术版社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧