CTE在Oracle和Sqlserver中使用的差异

        CTE是一个很好用的工具,他可以帮助我们清晰代码结构,减少临时表使用,同时oracle和sqlserver都提供支持。但在oracle和sqlserver中使用CTE也存在一定区别。

  • Oracle使用CTE方式

              1、CTE查询

          with cte as(
	     select 1 from dual 
    
             union all
	
             select 2 from dual
          )
          select * from cte;

              2、CTE插入

          insert into test
          with cte as(
	     select 1 from dual 
    
             union all
	
             select 2 from dual
          )
          select * from cte;

              3、CTE更新

          UPDATE  EMP_TRACKING_LIST  L
          SET ACTIVE_FLAG = 
          (
              WITH  sub_q  AS
              (
                  SELECT  ...
              )
              SELECT  ...
              FROM    sub_q  ...
          );

               4、CTE删除

          delete TCH_SCHTIME_LESSON
          where a in 
          (
              with c as (
	          Select * 
                  from b
              )
              select Tchlessonkey
              from c
              where 1 = 2
          )
          and Tchlessonkey <> 1; 


  • sqlserver使用CTE方式

             1、查询方式

          with cte as(
	     select 1 a 
    
             union all
	
             select 2 a
          )
          select * from cte;

             2、插入方式

          with cte as(
	     select 1 a
    
             union all
	
             select 2 a
          )

          insert into test

          select * from cte;

             3、更新方式

          with cte as(
	     select 1 a
    
             union all
	
             select 2 a
          )

          update test
          set b = a
          from cte;
 

             4、删除方式

          with cte as(
	     select 1 a
    
             union all
	
             select 2 a
          )

          delete 
          from test,cte
          where b = a;

       总结:在sqlserver中,cte非常强大,比较好用可以广泛应用于select,insert,update,delete中。

                   在oracle中,cte仅仅用于查询操作,并不能灵活使用在insert,update,delete中,建议使用merge

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值