我现在有一个非常复杂的逻辑,想要用存储过程实现。
我要从一个已存在的table中根据一些字段选出一个结果集,并根据部分字段,分别用每条记录去向其他两个表中插入数据。
目前确定使用游标。
逻辑的内容简单表示如下(这是伪码):
if table1.字段1 is not null
do ……
if table1.字段2 is not null
do ……
insert into table2 ……
insert into table3 ……
else if table1.字段3 is not null
do ……
insert into table2 ……
insert into table3 ……
endif
endif
else if table1.字段2 is not null
do ……
if table1.字段3 is not null
do ……
insert into table2 ……
insert into table3 ……
endif
endif
这种if的嵌套判断非常深,外面还需要个使用游标的循环。用存储过程和在程序中实现,效率上有什么不同吗?哪个更快?最好能简要说一说原因,谢谢。
顺便问一下,影响SQL语句和存储过程执行效率的因素通常有哪些?