常用的SQL语句

elect next_day(trunc(sysdate, 'mm'), 2) from dual; 
--创建存储过程

CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p

(

   --参数IN表示输入参数,

    --OUT表示输入参数,类型可以使用任意Oracle中的合法类型。

   is_ym  IN CHAR

)

AS

--定义变量

vs_msg   VARCHAR2(4000);   --错误信息变量

vs_ym_beg  CHAR(6);      --起始月份

vs_ym_end  CHAR(6);      --终止月份

vs_ym_sn_beg CHAR(6);     --同期起始月份

vs_ym_sn_end CHAR(6);     --同期终止月份



--定义游标(简单的说就是一个可以遍历的结果集)

CURSOR cur_1 IS

  SELECT area_code,CMCODE,SUM(rmb_amt)/10000 rmb_amt_sn,SUM(usd_amt)/10000 usd_amt_sn

  FROM BGD_AREA_CM_M_BASE_T

    WHERE ym >= vs_ym_sn_beg

       AND ym <= vs_ym_sn_end

   GROUP BY area_code,CMCODE;



BEGIN

--用输入参数给变量赋初值,用到了Oralce的SUBSTR TO_CHAR ADD_MONTHS TO_DATE 等很常用的函数。

vs_ym_beg := SUBSTR(is_ym,1,6);

vs_ym_end := SUBSTR(is_ym,7,6);

vs_ym_sn_beg := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_beg,'yyyymm'), -12),'yyyymm');

vs_ym_sn_end := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_end,'yyyymm'), -12),'yyyymm');

--先删除表中特定条件的数据。

DELETE FROM xxxxxxxxxxx_T WHERE ym = is_ym;

  --然后用内置的DBMS_OUTPUT对象的put_line方法打印出影响的记录行数,其中用到一个系统变量SQL%rowcount

DBMS_OUTPUT.put_line('del上月记录='||SQL%rowcount||'条');



INSERT INTO xxxxxxxxxxx_T(area_code,ym,CMCODE,rmb_amt,usd_amt)

SELECT area_code,is_ym,CMCODE,SUM(rmb_amt)/10000,SUM(usd_amt)/10000

FROM BGD_AREA_CM_M_BASE_T

  WHERE ym >= vs_ym_beg

  AND ym <= vs_ym_end

GROUP BY area_code,CMCODE;



DBMS_OUTPUT.put_line('ins当月记录='||SQL%rowcount||'条');

--遍历游标处理后更新到表。遍历游标有几种方法,用for语句是其中比较直观的一种。

FOR rec IN cur_1 LOOP

  UPDATE xxxxxxxxxxx_T

  SET rmb_amt_sn = rec.rmb_amt_sn,usd_amt_sn = rec.usd_amt_sn

   WHERE area_code = rec.area_code

   AND CMCODE = rec.CMCODE

   AND ym = is_ym;

END LOOP;



COMMIT;

--错误处理部分。OTHERS表示除了声明外的任意错误。SQLERRM是系统内置变量保存了当前错误的详细信息。

EXCEPTION

   WHEN OTHERS THEN

      vs_msg := 'ERROR IN xxxxxxxxxxx_p('||is_ym||'):'||SUBSTR(SQLERRM,1,500);

   ROLLBACK;

   --把当前错误记录进日志表。

   INSERT INTO LOG_INFO(proc_name,error_info,op_date)

   VALUES('xxxxxxxxxxx_p',vs_msg,SYSDATE);

   COMMIT;

   RETURN;

END;

CREATE OR REPLACE PROCEDURE P_ins_login_info

IS
  s_login_id   NUMBER(22);
  s_account_id NUMBER(22);
  s_persn_id   NUMBER(22);
  s_ply_id     NUMBER(22);
  s_helt_id    NUMBER(22);
  CURSOR Cur_ins_login_info IS
    select ins_id, accout_code, mobile, email, name, phone, b_dpt
      from ins_login_info t
     where t.mobile != 'AB'
     and t.mobile in ('15915693223', '15975130839', '18666315868');

  l_ins_login_info Cur_ins_login_info%ROWTYPE;

BEGIN
  --WIRTE BACK APPROVED
  FOR l_ins_login_info IN Cur_ins_login_info LOOP
    s_login_id := seq_app_login.nextval;
    ---登录信息
    insert into LOGIN_INFO
      (N_LOGIN_ID,
       N_LEVEL_ID,
       N_CUSTOMER_TYPE_ID,
       LOGIN_ACCOUNT,
       LOGIN_PASSWORD,
       MOBILE,
       EMAIL,
       N_STATUS,
       D_CREATE_DATE,
       N_CREATED,
       D_MODIFY_DATE,
       N_MODIFIED)
    values
      (s_login_id,
       382,
       1,
       l_ins_login_info.mobile,
       Lower(MD5(l_ins_login_info.mobile)),
       l_ins_login_info.mobile,
       l_ins_login_info.email,
       1,
       sysdate,
       22,
       sysdate,
       22);
    ---账户信息
    s_account_id := seq_app_account.nextval;
    insert into ACCOUNT_INFO
      (N_ACCOUNT_ID,
       N_CUSTOMER_TYPE_ID,
       N_LOGIN_ID,
       ACCOUNT_LOGIN,
       NAME,
       MOBILE,
       EMAIL,
       ADDRESS,
       D_CREATE_DATE,
       N_STATUS,
       D_MODIFY_DATE,
       CONSUMPTION_AMOUNT)
    values
      (s_account_id,
       1,
       s_login_id,
       l_ins_login_info.mobile,
       l_ins_login_info.name,
       l_ins_login_info.mobile,
       l_ins_login_info.email,
       l_ins_login_info.b_dpt,
       sysdate,
       1,
       sysdate,
       0.00);
    ---增加基本信息
    s_persn_id := seq_app_login.nextval;
    insert into PERSONAL_BASIC_INFO
      (N_PERSONAL_ID,
       N_LOGIN_ID,
       NAME,
       MOBILE,
       EMAIL,
       D_CREATE_DATE,
       D_MODIFY_DATE)
    values
      (s_persn_id,
       s_login_id,
       l_ins_login_info.name,
       l_ins_login_info.mobile,
       l_ins_login_info.email,
       sysdate,
       sysdate);
    ---增加个性化信息
    s_ply_id := seq_app_login.nextval;
    insert into PERSONALITY_INFO
      (N_PERSONALITY_ID,
       N_LOGIN_ID,
       N_RANK_ID,
       NICKNAME,

       D_CREATE_DATE,
       D_MODIFY_DATE,
       LASTYEAR_AMOUNT)
    values
      (s_ply_ID,
       s_login_id,
       261,
       l_ins_login_info.name,
       sysdate,
       sysdate,
       0.00);
    ---增加健康信息
    s_helt_id := seq_app_login.nextval;
    insert into HEALTH_YGENERIC_INFO
      (N_HEALTH_YGENERIC_ID,
       N_LOGIN_ID,
       N_MARITAL_STATUS,
       D_CREATE_DATE,
       D_MODIFY_DATE)
    values
      (s_helt_id, s_login_id, 2, sysdate, sysdate);

    COMMIT;

  END LOOP;

EXCEPTION
  WHEN OTHERS THEN
    ROLLBACK;

END P_ins_login_info;

你可能感兴趣的:(sql)