表记录的插入方式有两种。其一,先create table 再 insert into from ...。其二, 直接 select into。

第一种方式,由于要记录日志,因此IO消耗更多,duration 更长。一般来说能用 select into 的,就尽量不要用 insert into的方式。

有时,存储过程中会需要,根据不同的条件,从不同的表中获取数据放入一个临时表。看起来,这样就需要在不同的分支语句中,写多个对同一张的 select into 语句。

例如:

if (@b=1)

begin

select  a.id, a.name, b.price

into #temp

from A inner join B on (a.id=b.id)

end else if (@b=2)

begin

select  d.id, d.name, c.price

into #temp

from D inner join C on (d.id=c.id)

end

但创建存储过程时会报错,说 #temp 表已经存在。

怎么解决呢?

方法一:用第一种方式,问题是性能差;

方法二:偷懒一些,但性能更好的方法

select  a.id, a.name, b.price

into #tempA

from A inner join B on (@b=1 and a.id=b.id)

where @b=1

select  d.id, d.name, c.price

into #tempB

from D inner join C on (@b=2 and d.id=c.id)

where @b=2

select *

into #temp

from ( select * from #tempA union all select * from #tempB )

方法三:用动态sql的办法,把所有的语句都拼接好。好处是性能比方法二好,但缺点也很明显,可读性不强;

方法四:其实,不同的功能,还是最好分成不同的存储过程,优化,维护都更简单。

不知道有没有更好的方式?

最新文章

  1. springmvc使用freemarker
  2. Eclipse 项目中有红色感叹号,怎么办?
  3. 官方提供的屏蔽百度转码Baidu Transcoder的方法no-transform
  4. MSSQL订阅库索引对齐
  5. epoll重要
  6. 【待补】java开发Web Service
  7. 关闭归档提示:ORA-38774: cannot disable media recovery - flashback database is enabled
  8. linux中curl命令
  9. ADN中国队参加微软Kinect他赢得了全国比赛三等奖,我们的创意项目与团队Kinect于Naviswork虚拟之旅
  10. IE中的事件对象
  11. php 中数据类型
  12. 修改LibreOffice Draw中定义的样式名称
  13. (30)批处理文件.bat
  14. JAVA中子类会不会继承父类的构造方法
  15. Assignments
  16. C语言_简单了解一下typedef
  17. react-native中的触摸事件
  18. docker中i的作用
  19. jenkins在windows平台自动化构建代码
  20. leecode第八十八题(合并两个有序数组)

热门文章

  1. Andy's First Dictionary
  2. hdu 2842 Chinese Rings
  3. JAVA NIO 选择器
  4. 【BASH】自己主动清理rman脚本备份文件
  5. Andriod中绘(画)图----Canvas的使用具体解释
  6. Hlg 1832 【线段树 && RMQ】.cpp
  7. UVA 11768 - Lattice Point or Not(数论)
  8. I2C操作笔记——以 AT24C04为例
  9. WPF-20:richtextbox相关操作(转)
  10. HDU 3304 Interesting Yang Yui Triangle lucas定理