博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
同一个存储过程中,不能多次select into 到同一张表的问题
阅读量:7017 次
发布时间:2019-06-28

本文共 877 字,大约阅读时间需要 2 分钟。

表记录的插入方式有两种。其一,先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的办法,把所有的语句都拼接好。好处是性能比方法二好,但缺点也很明显,可读性不强;

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

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

  

转载地址:http://eszxl.baihongyu.com/

你可能感兴趣的文章
js格式化日期
查看>>
详解BSCI实验五、配置PIM auto-rp
查看>>
SO_DONTROUTE和SO_BINDTODEVICE的深层次分析
查看>>
WINserver路由服务之多网段管控
查看>>
网络基础CCNP|SDN与日志
查看>>
Python3.X Socket 一个编码与解码的坑
查看>>
vs2015未能正确加载“ProviderPackage”包。
查看>>
PHP带头大哥讲解几种综合PHP网络服务器系统的选择!
查看>>
MySQL数据表所有操作命令
查看>>
使用SQLRootKit网页数据库后门控制案例
查看>>
Jmeter性能测试-----参数化方法CSVRead函数
查看>>
iptables的备份及脚本构成
查看>>
二级缓存相关属性
查看>>
Python内置容器(1)——列表,元组,集合
查看>>
ASP.NET中实现回调
查看>>
OC学习笔记[注意事项]
查看>>
使用Spinner和setDropDownViewResource
查看>>
机房日常技术总结——Linux篇
查看>>
如何从计算机中删除 Exchange Server 2003
查看>>
邮件服务器的基础知识概述
查看>>