oracle for循环示例(for in loop)
方式一:循环游标
CREATE OR REPLACE PROCEDURE "PRO_ADATA_CHECK_TABLE2"(
IN_ORG_CODE VARCHAR2,
IN_BATCH_CODE VARCHAR2,
OUT_TABLENAME OUT VARCHAR2 )
IS --设置变量
V_COUNT NUMBER DEFAULT 0; --数据量
V_COUNT_SQL VARCHAR2(500); --查询全部语句
V_TABLE_NAME_STR VARCHAR2(500); -- 不存在库中的表名
BEGIN
-- 根据当前机构查询出需要验证的表,循环查一下这些表是否在库中存在
FOR j IN
(SELECT T.TABLENAME FROM META_ORG_TABLE T WHERE T.ORGCODE = IN_ORG_CODE
)
LOOP
V_COUNT_SQL :='SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = UPPER(:1)';
EXECUTE IMMEDIATE V_COUNT_SQL INTO V_COUNT USING j.TABLENAME;
IF V_COUNT = 0 --即表不存在,插入到日志表中
THEN
INSERT INTO ZYK_DATE_CHECK
(
ID,
ORGCODE,
TABLE_NAME,
DATA_NUM,
CHECK_DATE,
BATCH_CODE,
REMARK
)
VALUES (
SEQ_ZYK_DATE_CHECK.NEXTVAL ,
IN_ORG_CODE,
j.TABLENAME,
0,
SYSDATE,
IN_BATCH_CODE,
'此表不存在库中!' );
COMMIT;
IF V_TABLE_NAME_STR IS NULL THEN
V_TABLE_NAME_STR := j.TABLENAME;
ELSE
V_TABLE_NAME_STR := V_TABLE_NAME_STR || ';' || j.TABLENAME;
dbms_output.put_line(V_TABLE_NAME_STR);
END IF;
END IF;
END LOOP;
OUT_TABLENAME := V_TABLE_NAME_STR;
END PRO_ADATA_CHECK_TABLE;
2022年10月13日18:33:49
方式二:循环数字
FOR I IN 1 .. 100 LOOP
--循环操作
dbms_output.put_line(i);
END LOOP;
2023年3月10日09:58:58
其它示例
CREATE OR REPLACE PROCEDURE PRO_DIAGNOSIS_PREINFO(IN_DATE IN VARCHAR2) IS
--YYYY-MM-DD
V_DATE VARCHAR2(10);
BEGIN
--V_DATE赋值
IF IN_DATE IS NULL THEN
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') INTO V_DATE FROM DUAL;
ELSE
V_DATE := IN_DATE;
END IF;
FOR ROW_DATA IN (SELECT '' AS TELME_PRE_NUM, -- 参会人员信息编号
T.SQDID AS TELME_ASK_NUM, -- 远程医疗服务申请单编号
'' AS TELME_ASK_TCODE, -- 远程医疗服务申请单类别代码
DECODE(T.IDENTIFIER,
'XDXT_01',
'111',
'XDXT_02',
'222',
'') AS PAR_ORG_CODE, -- 参与远程会诊的医疗机构代码
DECODE(T.IDENTIFIER,
'XDXT_01',
'市中医院',
'XDXT_02',
'市人民医院',
'') AS PAR_ORG_NAME, -- 参与远程会诊的医疗机构名称
DECODE(T.IDENTIFIER,
'XDXT_01',
'县级',
'XDXT_02',
'县级',
'') AS PAR_ORG_LEVEL, -- 参与远程会诊的医疗机构级别
T.DIAGDEPTCODE AS PAR_DEP_CODE, -- 参与远程会诊的方科室代码
T.DIAGDEPTNAME AS INV_DEP_NAME, -- 参与远程会诊的科室名称
T.DIAGDOCCODE AS PAR_EXPERT_NUM, -- 参与方专家编号
T.DIAGDOCNAME AS PAR_EXPERT_NAME, -- 参与远程会诊的专家签名
'' AS PAR_DOC_RANK -- 参与远程会诊的医生职级
FROM XD_PATIENT_INFO_TS T
WHERE T.CREATETIME >= TO_DATE(V_DATE, 'YYYY-MM-DD')
AND T.CREATETIME < TO_DATE(V_DATE, 'YYYY-MM-DD') + 1) LOOP
INSERT INTO STANDARD_YCYL.REMOTEECG_DIAGNOSIS_PREINFO
(TELME_PRE_NUM,
TELME_ASK_NUM,
TELME_ASK_TCODE,
PAR_ORG_CODE,
PAR_ORG_NAME,
PAR_ORG_LEVEL,
PAR_DEP_CODE,
INV_DEP_NAME,
PAR_EXPERT_NUM,
PAR_EXPERT_NAME,
PAR_DOC_RANK)
VALUES
(ROW_DATA.TELME_PRE_NUM,
ROW_DATA.TELME_ASK_NUM,
ROW_DATA.TELME_ASK_TCODE,
ROW_DATA.PAR_ORG_CODE,
ROW_DATA.PAR_ORG_NAME,
ROW_DATA.PAR_ORG_LEVEL,
ROW_DATA.PAR_DEP_CODE,
ROW_DATA.INV_DEP_NAME,
ROW_DATA.PAR_EXPERT_NUM,
ROW_DATA.PAR_EXPERT_NAME,
ROW_DATA.PAR_DOC_RANK);
COMMIT;
END LOOP;
END PRO_DIAGNOSIS_PREINFO;
写在最后
哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!
相关推荐:
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/16407786.html