Jcat
          寵辱不驚,閑看庭前花開花落~~
          posts - 173,comments - 67,trackbacks - 0

          1.? top N 問題
          在sql server中,top N 問題很容易解決,如下例:從表stbdbdj中選取排序后的第一行數據進行賦值。

          在sql中解決方法很簡單,在select 后面加上:top n 即可,其中 n 代表行數。

          select?top?1?@entrust_date?=?entrust_date,
          @entrust_no?=?entrust_no
          from?run2k..stbdbdj
          where?entrust_date?=?@date
          and?entrust_no?>?@entrust_no_q
          and?report_status?=?'1'
          order?by?entrust_date,entrust_no;


          在oracle中,沒有top n這個命令,我們采取把兩層查詢方式解決:首先,把需要查找的字段值直接進行排序,然后在外面進行第二次查詢,并使用rownum決定行數。

          select?entrust_date,entrust_no
          into?@entrust_date,?@entrust_no
          from?(?select?entrust_date,entrust_no
          from?stbdbdj
          where?entrust_date?=?@date
          and?entrust_no?>?@entrust_no_q
          and?report_status?=?'1'
          order?by?entrust_date,entrust_no?)
          where?rownumber?<=1?;



          2. 如何解決結果集返回時,* 和變量同時存在的問題
          下面例子表示,在用游標返回結果集時,同時返回一個變量的值,在
          sql server 中代碼如下所示:
          select?a.*,b.organ_id
          from?run2k..stbbp?a,run2k..stkaccoarg?b
          where?a.date?=?@entrust_date
          and?a.serial_no?=?@serial_no
          and?a.branch_no?=?b.branch_no
          and?a.exchange_type?=?b.exchange_type;

          但在oracle中卻沒有這種用法,’*’后面必需跟from。解決方法如下:
          1)我們可以把 '*' 變成所需要選擇的字段,就是說采用表中需要顯示的全部字段表示*。
          例如:

          open ?p_cursor? for
          select ?branch_no,...,organ_id
          where ...

          2)如果這個字段或者說變量是從另外一張表中取出來的,同樣可以采用下面的辦法。

          open?p_cursor?for
          select?a.*,b.organ_id;
          from?stkaccoentrust?a,?stkaccoarg?b
          where?a.branch_no?=?b.branch_no
          and?a.exchange_type?=?b.exchange_type
          and?a.init_date?=?v_entrust_date
          and?a.serial_no?=?v_serial_no;

          3. 外聯接問題
          sql
          <---> oracle
          a = *b <---> a(+)= b
          a *= b <---> a = b(+)

          4. 多條記錄求和問題
          select sum(A+B+C)
          into D
          from ...
          where ...
          group by ...

          單條記錄求和
          select A+B
          into C
          from ...
          where ...

          5. case 問題轉換
          sql:
          case client_status
          when '0' then '正常'
          when '1' then '凍結'
          when '2' then '掛失'
          when '3' then '銷戶'
          else '未知'
          end

          oracle:
          decode(client_status,'0','正常,'1','凍結','2','掛失','3','銷戶','未知');

          6. char 和 varchar 類型區別:
          char 尾部補空格,varchar 尾部不補空格。

          7. convert轉換問題

          sql
          ---> oracle
          convert(char(5),branch_no) ---> to_char(branch_no,'99999')
          convert(char(19),count(*)) ---> lpad(to_char(count(*)),19)
          convert(varchar(20),serial_no) ---> to_char(serial_no,'999...9' )
          總共20個9
          lpad(to_char(serial_no),20)


          8. charindex(substring,string) ---> instr(string,substring)
          子串 父串 ---> 父串 子串

          posted on 2007-09-13 15:42 Jcat 閱讀(2414) 評論(0)  編輯  收藏 所屬分類: Database
          主站蜘蛛池模板: 泸西县| 云龙县| 深圳市| 邵东县| 睢宁县| 南雄市| 开鲁县| 黄浦区| 汪清县| 焦作市| 龙游县| 鞍山市| 库尔勒市| 武清区| 赤峰市| 金华市| 乐亭县| 南部县| 泰顺县| 垣曲县| 长葛市| 嵩明县| 滨海县| 山阴县| 旌德县| 沙洋县| 桓仁| 尚义县| 和林格尔县| 保定市| 邢台县| 辽中县| 阿荣旗| 五寨县| 迭部县| 靖边县| 株洲市| 胶州市| 内丘县| 卢龙县| 广东省|