我正在写一个存储过程,它是一个动态的续集语句,可以从一个中间表写入另一个表。我在编写sql和存储过程特别是新手。我尝试在语句中添加参数,但始终收到此错误: ORA-00936:缺少表达 ORA-06512:位于“ ECH_ETL_BATCH_ID.LOOKUP_TABLE_INSERT”的第26行 ORA-06512:在第14行 流程已退出。 我拿出参数并在值中进行了硬编码,只是试图使其正常工作,但我仍然收到相同的错误。 有什么建议吗?
create or replace PROCEDURE
LOOKUP_TABLE_INSERT
(
P_SOURCE_DB IN VARCHAR2
, P_SOURCE_TABLE IN VARCHAR2
, P_TARGET_DB IN VARCHAR2
, P_TARGET_TABLE IN VARCHAR2
, P_COLUMN_NAME IN VARCHAR2
) AS
l_sql_statement VARCHAR2(1000);
BEGIN
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP' ||
'(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID,
HUB_STATE_IND, ROLE_TP) ' ||
' SELECT MISSING_VALUES AS PKEY_SRC_OBJECT' ||
', 1 as VERSION_SEQ' ||
', 0 as TIMELINE_ACTION' ||
', INSERT_TS as LAST_UPDATE_DATE' ||
', MISSING_VALUES as "SRC_ROWID"' ||
', 1 as "HUB_STATE_IND"' ||
', MISSING_VALUES as ROLE_TP' || --P_COLUMN_NAME ||
' FROM ECH_ETL_BATCH_ID.Ref_Intermediate ' ||
WHERE COLUMN_NM =' || ''||ROLE_TP||'' ||
'AND LOOKUP_TBL_NM =' || ''||C_S_LU_PTY_ROLE_TP||'';
dbms_output.put_line(l_sql_statement);
EXECUTE IMMEDIATE l_sql_statement;
COMMIT;
END;
EXECUTE IMMEDIATE l_sql_statement;
COMMIT;
END;
答案 0 :(得分:0)
您的问题在于where条件。删除||
如果您执行dbms_output.putline();
,则会得到该陈述
要添加单引号,请检查以下解决方案
错误原因:
INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP
(PKEY_SRC_OBJECT,
VERSION_SEQ,
TIMELINE_ACTION,
LAST_UPDATE_DATE,
SRC_ROWID,
HUB_STATE_IND,
ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT,
1 as VERSION_SEQ,
0 as TIMELINE_ACTION,
INSERT_TS as LAST_UPDATE_DATE,
MISSING_VALUES as "SRC_ROWID",
1 as "HUB_STATE_IND",
MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM = || ROLE_TP ||
AND LOOKUP_TBL_NM = || C_S_LU_PTY_ROLE_TP ||
解决方案:
create or replace PROCEDURE
LOOKUP_TABLE_INSERT
(
P_SOURCE_DB IN VARCHAR2
, P_SOURCE_TABLE IN VARCHAR2
, P_TARGET_DB IN VARCHAR2
, P_TARGET_TABLE IN VARCHAR2
, P_COLUMN_NAME IN VARCHAR2
) AS
l_sql_statement VARCHAR2(1000);
BEGIN
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP' ||
'(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID,
HUB_STATE_IND, ROLE_TP) ' ||
' SELECT MISSING_VALUES AS PKEY_SRC_OBJECT' ||
', 1 as VERSION_SEQ' ||
', 0 as TIMELINE_ACTION' ||
', INSERT_TS as LAST_UPDATE_DATE' ||
', MISSING_VALUES as "SRC_ROWID"' ||
', 1 as "HUB_STATE_IND"' ||
', MISSING_VALUES as ROLE_TP' || --P_COLUMN_NAME ||
' FROM ECH_ETL_BATCH_ID.Ref_Intermediate ' ||
' WHERE COLUMN_NM =''' ||ROLE_TP||''
' AND LOOKUP_TBL_NM =''' ||C_S_LU_PTY_ROLE_TP|''' ';
dbms_output.put_line(l_sql_statement);
EXECUTE IMMEDIATE l_sql_statement;
COMMIT;
END;
/
答案 1 :(得分:0)
(已编辑)代码生成的动态语句为:
INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID,
HUB_STATE_IND, ROLE_TP) SELECT MISSING_VALUES AS PKEY_SRC_OBJECT, 1 as VERSION_SEQ, 0 as TIMELINE_ACTION, INSERT_TS as LAST_UPDATE_DATE, SELECT MISSING_VALUES as "SRC_ROWID", 1 as "HUB_STATE_IND", MISSING_VALUES as ROLE_TP FROM ECH_ETL_BATCH_ID.Ref_Intermediate WHERE COLUMN_NM =||ROLE_TP|| AND LOOKUP_TBL_NM =||C_S_LU_PTY_ROLE_TP||
或分成多行并加上注释以显示其存在的问题:
INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP(PKEY_SRC_OBJECT, VERSION_SEQ,
TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID, HUB_STATE_IND, ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT
, 1 as VERSION_SEQ
, 0 as TIMELINE_ACTION
, INSERT_TS as LAST_UPDATE_DATE
, SELECT MISSING_VALUES as "SRC_ROWID"
--^^^^^^
, 1 as "HUB_STATE_IND"
, MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM =||ROLE_TP|| AND LOOKUP_TBL_NM =||C_S_LU_PTY_ROLE_TP||
-- ^^ ^^ ^^ ^^
您对此有一些问题;多数情况下,您在生成的语句中的奇怪位置嵌入了||
的集合,但是您还多了SELECT
。
这可能更接近您想要的内容,包括嵌入式字符串文字的转义单引号:
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP' ||
'(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID, HUB_STATE_IND, ROLE_TP) ' ||
' SELECT MISSING_VALUES AS PKEY_SRC_OBJECT' ||
', 1 as VERSION_SEQ' ||
', 0 as TIMELINE_ACTION' ||
', INSERT_TS as LAST_UPDATE_DATE' ||
', MISSING_VALUES as "SRC_ROWID"' ||
', 1 as "HUB_STATE_IND"' ||
', MISSING_VALUES as ROLE_TP' ||
' FROM ECH_ETL_BATCH_ID.Ref_Intermediate ' ||
' WHERE COLUMN_NM = ''ROLE_TP''' ||
' AND LOOKUP_TBL_NM = ''C_S_LU_PTY_ROLE_TP''';
会生成(再次拆分):
INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP(PKEY_SRC_OBJECT, VERSION_SEQ,
TIMELINE_ACTION, LAST_UPDATE_DATE, SRC_ROWID, HUB_STATE_IND, ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT
, 1 as VERSION_SEQ
, 0 as TIMELINE_ACTION
, INSERT_TS as LAST_UPDATE_DATE
, MISSING_VALUES as "SRC_ROWID"
, 1 as "HUB_STATE_IND"
, MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM = 'ROLE_TP'
AND LOOKUP_TBL_NM = 'C_S_LU_PTY_ROLE_TP'
您可以将字符串分配拆分为多行,这样可以简化事情,因为这会减少(或现在删除)字符串连接:
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP
(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE,
SRC_ROWID, HUB_STATE_IND, ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT
, 1 as VERSION_SEQ
, 0 as TIMELINE_ACTION
, INSERT_TS as LAST_UPDATE_DATE
, MISSING_VALUES as "SRC_ROWID"
, 1 as "HUB_STATE_IND"
, MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM = ''ROLE_TP''
AND LOOKUP_TBL_NM = ''C_S_LU_PTY_ROLE_TP''';
或者您可以使用the alternative quoting mechanism来避免转义单引号。
创建动态语句时,请从有效的静态语句开始,然后向后进行;并使用dbms_output
显示最终生成的字符串,以便您可以进行验证并在需要时手动运行它。
当然,这根本不需要动态,但是从您最初的问题开始,您打算在某个时候在子查询中使用过程参数。
要使用过程参数而不是必须使用的字符串文字,可以将这些参数串联在一起:
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP
(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE,
SRC_ROWID, HUB_STATE_IND, ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT
, 1 as VERSION_SEQ
, 0 as TIMELINE_ACTION
, INSERT_TS as LAST_UPDATE_DATE
, MISSING_VALUES as "SRC_ROWID"
, 1 as "HUB_STATE_IND"
, MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM = ' || P_COLUMN_NAME || '
AND LOOKUP_TBL_NM = ' || P_TARGET_TABLE;
或最好使用绑定变量:
l_sql_statement :=
'INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP
(PKEY_SRC_OBJECT, VERSION_SEQ, TIMELINE_ACTION, LAST_UPDATE_DATE,
SRC_ROWID, HUB_STATE_IND, ROLE_TP)
SELECT MISSING_VALUES AS PKEY_SRC_OBJECT
, 1 as VERSION_SEQ
, 0 as TIMELINE_ACTION
, INSERT_TS as LAST_UPDATE_DATE
, MISSING_VALUES as "SRC_ROWID"
, 1 as "HUB_STATE_IND"
, MISSING_VALUES as ROLE_TP
FROM ECH_ETL_BATCH_ID.Ref_Intermediate
WHERE COLUMN_NM = :COLUMN_NAME
AND LOOKUP_TBL_NM = :TARGET_TABLE';
EXECUTE IMMEDIATE l_sql_statement USING P_COLUMN_NAME, P_TARGET_TABLE;
但这仍然不需要动态。