zoukankan      html  css  js  c++  java
  • oracle和sqlserver在过程sql中通过select对变量进行赋值的区别及实例

    变量的声明:

    • oracle的变量必须在在declare部分声明——意思是必须建立begin end块,块结构也强制变量必须先声明后使用,即oracle变量在过程内是有不同作用域、不同的生存期的;同一变量可以在不同的作用域内定义多次,内层的会override外层的定义;
    • sqlserver的变量可以在过程的任何部分声明——sqlserver有且只有一个作用域,但只有声明后的部分才可以使用变量——不同的生存期;

    变量的初始化:

    • oracle可以在声明是设置默认值;默认初始化值为null
    • sqlserver不能在声明是设置默认值;默认初始化值为null

    select赋值的语法

    • oracle为 select expr,...  into variable,...  from table where ....
    • sqlserver为 select @variable=expr,... from table where ...

    错误处理:

    • oracle通过select ... into  ... from ... 赋值会产生错误!必须有错误处理,过程才能成功执行,否则过程抛出错误给应用程序;变量的值不受该错误操作的影响!

    无值——变量保持select ... into  ... from ...之前的状态

    多值——变量保持select ... into  ... from ...之前的状态

    •  sqlserver的select @variable=expr不会产生错误,无需错误处理;但变量的值受影响

    无值——变量保持select @variable=expr之前的状态

    多值——变量的值实际上是select expr 结果集最后一行的记录的值,而不是第一行。

    实例代码如下:

    create or replace procedure pro_mrtom_zys_error_upd(beginDate varchar2,endDate varchar2,taxtypeId varchar2,rightRate number)
    is
    --定义变量
    totalMoney number(18,4);  --销售金额
    transMoney number(18,4);  --运输金额
    begin
      --油标
      Declare
       Cursor curCoalSellTax is (select
       cs.ticketnumber,cst.taxtypeid,cs.sellquantity,cs.sellunitprice,cs.transitunitprice,cs.selltypeid
       from coalsell cs
       inner join coalselltax cst on(cst.ticketnumber=cs.ticketnumber and cst.wordrail=cs.wordrail)
       inner join taxtype tt on(tt.id=cst.taxtypeid)
       left join taxtype ptt on(tt.parentid=ptt.id)
       where cs.status=0 and cs.recordtype=0 and cst.taxtypeid=taxtypeId
       and cs.filldate between to_date(beginDate,'yyyy-MM-dd') and to_date(endDate,'yyyy-MM-dd')
       and cs.sellquantity * rightRate <> cst.taxcharge);
       Begin   --油标开始
       for currRow in curCoalSellTax --循环取得每行数据--
       Loop
            begin -- 油标循环开始
                select count(*),count(*) + 1 into totalMoney,transMoney from taxtype;
                --直接方式
                --transMoney := totalMoney;           
                dbms_output.put_line('总条数:'||totalMoney || '---' ||transMoney);
                --内销判断
                if currRow.selltypeid = 'SDFSDFSDFDADA' then
                    dbms_output.put_line('内销');
                end if;
                --外销判断
                if currRow.selltypeid = 'ASDAWFWAFAFA' then
                    dbms_output.put_line('外销');
                end if;
            end;
       end Loop;
       end curCoalSellTax;
    end pro_mrtom_zys_error_upd;

    Mr.Tom@live.cn
  • 相关阅读:
    Maven发布工程到公共库
    js一些代码方法
    我的语录
    java 安装配置时出现的问题
    HDUOJ----4501小明系列故事——买年货(三维背包)
    message 弹出窗口
    2014校招 百度试题及答案
    HDUOJ---1862EXCEL排序
    HDUOJ-----3591The trouble of Xiaoqian
    HDUOJ-----2571跳舞毯
  • 原文地址:https://www.cnblogs.com/mrtom/p/2086150.html
Copyright © 2011-2022 走看看