SQL Query在执行时工作正常,但如果我在PL / SQL中使用它会显示错误,则查询相同

时间:2014-06-07 15:01:40

标签: sql oracle plsql

您好我以下查询在SQL中运行正常,我的意思是它成功执行。

SELECT id_ligne, n_res_id, n_rty_id, NVL (res_va_text, va_res_txt),
       NVL (res_va_short_name, va_res_short_name), bl_has_bo, bl_asset,
       bl_needs_profile, bl_update_name, bl_popup_mandatory, va_popup_name,
       va_itrack_create, va_itrack_delete, va_generic_mail,
       n_application_type, bl_mono_profile
  FROM t_t_actres_ea4, t_resource_ea4
 WHERE res_n_id(+) = n_res_id
   AND t_t_actres_ea4.id_ligne NOT IN (
          SELECT DISTINCT (t_erralim_ea4.err_n_lineid)
                     FROM t_erralim_ea4
                    WHERE 0235303 = t_erralim_ea4.err_n_code_trt
                      AND err_n_rejecttype = 0)

它被执行并获取所需的输出。但是当我对PL / SQL使用相同的查询时,如下所示。

L_REQ Varchar2(1000) := 'select ID_LIGNE, N_RES_ID, N_RTY_ID, NVL(RES_VA_TEXT,VA_RES_TXT),NVL(RES_VA_SHORT_NAME,VA_RES_SHORT_NAME), '||
' BL_HAS_BO, BL_ASSET, BL_NEEDS_PROFILE, BL_UPDATE_NAME, BL_POPUP_MANDATORY, VA_POPUP_NAME, ' ||
' VA_ITRACK_CREATE, VA_ITRACK_DELETE, VA_GENERIC_MAIL, N_APPLICATION_TYPE, BL_MONO_PROFILE ' ||
' from T_T_ACTRES_EA4, T_RESOURCE_EA4  ' ||
' where RES_N_ID (+) = N_RES_ID AND T_T_ACTRES_EA4.ID_LIGNE not in ( select distinct(T_ERRALIM_EA4.ERR_N_LINEID) ' ||
' from T_ERRALIM_EA4 ' ||
' where 0235307 = T_ERRALIM_EA4.ERR_N_CODE_TRT AND ERR_N_REJECTTYPE = 0 and T_ERRALIM_EA4.ERR_VA_MSG  not like ''Mono-profile%'')';

它给出了错误 ORA-00936:表达式缺失

  

注意:当我将此条件附加到子查询时会导致此错误。   和T_ERRALIM_EA4.ERR_VA_MSG不喜欢''Mono-profile%''

TIA。

1 个答案:

答案 0 :(得分:0)

假设你提供的是你想要的,没有立即执行:它无法编译。

你有

variable declaration := sql statement

你应该

DECLARE
  variable declaration
BEGIN
  sql statement
END;

sql语句应具有SELECT INTO语法

SELECT id_ligne, n_res_id, n_rty_id, NVL (res_va_text, va_res_txt),
       NVL (res_va_short_name, va_res_short_name), bl_has_bo, bl_asset,
       bl_needs_profile, bl_update_name, bl_popup_mandatory, va_popup_name,
       va_itrack_create, va_itrack_delete, va_generic_mail,
       n_application_type, bl_mono_profile
  INTO LREQ
  FROM t_t_actres_ea4, t_resource_ea4
 WHERE res_n_id(+) = n_res_id
   AND t_t_actres_ea4.id_ligne NOT IN (
          SELECT DISTINCT (t_erralim_ea4.err_n_lineid)
                     FROM t_erralim_ea4
                    WHERE 0235303 = t_erralim_ea4.err_n_code_trt
                      AND err_n_rejecttype = 0);

@Bob Jarvis也是现货 - 你可以做到

DECLARE
 L_REQ Varchar2(1000) := 'select ID_LIGNE, N_RES_ID, N_RTY_ID, NVL(RES_VA_TEXT,VA_RES_TXT),NVL(RES_VA_SHORT_NAME,VA_RES_SHORT_NAME), '||
' BL_HAS_BO, BL_ASSET, BL_NEEDS_PROFILE, BL_UPDATE_NAME, BL_POPUP_MANDATORY, VA_POPUP_NAME, ' ||
' VA_ITRACK_CREATE, VA_ITRACK_DELETE, VA_GENERIC_MAIL, N_APPLICATION_TYPE, BL_MONO_PROFILE ' ||
' from T_T_ACTRES_EA4, T_RESOURCE_EA4  ' ||
' where RES_N_ID (+) = N_RES_ID AND T_T_ACTRES_EA4.ID_LIGNE not in ( select distinct(T_ERRALIM_EA4.ERR_N_LINEID) ' ||
' from T_ERRALIM_EA4 ' ||
' where 0235307 = T_ERRALIM_EA4.ERR_N_CODE_TRT AND ERR_N_REJECTTYPE = 0 and T_ERRALIM_EA4.ERR_VA_MSG  not like ''Mono-profile%'')';
BEGIN
   EXECUTE IMMEDIATE L_REQ;
END;

这可能不是最好的选择,静态SQL在很多时候提供了更好的性能。