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
          主站蜘蛛池模板: 牡丹江市| 南宫市| 余江县| 高安市| 河北省| 鹤山市| 嵊泗县| 华坪县| 吴忠市| 特克斯县| 老河口市| 宿州市| 苏尼特右旗| 安图县| 吴堡县| 崇左市| 磐安县| 沙河市| 江油市| 定陶县| 玛曲县| 莱州市| 万山特区| 襄樊市| 中超| 靖安县| 双峰县| 道真| 梨树县| 蚌埠市| 龙岩市| 遂宁市| 轮台县| 定州市| 诸暨市| 定兴县| 文水县| 宁夏| 平罗县| 金川县| 来安县|