如何在SQL * Plus中将函数中的参数(值)传递/调用到“选择”stmt?

时间:2012-05-08 17:12:39

标签: sql oracle plsql toad

我在将参数从我创建的函数传递到'Where'子句中的SQL * Plus'Select'语句时出现问题。但是,似乎我使用的任何语法都不正确或根本没有在这个'Where'子句中使用。

这是我创建的函数,看起来可以正常编译并运行良好:

CREATE OR REPLACE FUNCTION SA_BILL_CNTRL_EXTRNL_FXN(p_client_code      VARCHAR2
                                                   ,p_bill_seq         VARCHAR2
                                                   ,p_bill_control_seq VARCHAR2)
  RETURN VARCHAR2 IS
  v_external_id      VARCHAR2(30);
  v_client_code      VARCHAR2(4) := p_client_code;
  v_bill_seq         NUMBER := to_number(p_bill_seq);
  v_bill_control_seq NUMBER := to_number(p_bill_control_seq);
  v_cnt              NUMBER;
BEGIN
  SELECT COUNT(*)
  INTO v_cnt
  FROM BillControl@SQL_SAREP bc
  WHERE bc."ExternalID" IS NOT NULL
        AND bc."ExternalID" != ''
        AND bc."ClientCode" = p_client_code
        AND bc."BillControlSeq" = v_bill_control_seq
        AND bc."BillSeq" = v_bill_seq;
  IF v_cnt = 1 THEN
    SELECT bc."ExternalID"
    INTO v_external_id
    FROM BillControl@SQL_SAREP bc
    WHERE bc."ExternalID" IS NOT NULL
          AND bc."ExternalID" != ''
          AND bc."ClientCode" = p_client_code
          AND bc."BillControlSeq" = v_bill_control_seq
          AND bc."BillSeq" = v_bill_seq;
  ELSE
    v_external_id := NULL;
  END IF;
  RETURN v_external_id;
END SA_BILL_CNTRL_EXTRNL_FNX;
/

另外,当我在功能中使用'IN OUT'参数时,它在运行后失败。

现在,这是'Select'语句,我在传递参数时遇到困难:

SELECT NVL(TRIM(eb.CASE_NUM_REEVALUATED), TRIM(eb.CASE_NUM_DUPLICATED)) CaseNumReev
      ,gnc.BILL_CONTROL_SEQ
FROM EPE_Bill eb NETWORK_CROSSWALK gnc
WHERE substr(eb.MIC_BILL_ID, 5, 4) = SA_BILL_CNTRL_EXTRNL_FNX(p_client_code)
      AND gnc.BILL_CONTROL_SEQ = SA_BILL_CNTRL_EXTRNL_FNX(p_bill_seq) 
      AND (to_number(substr(eb.MIC_BILL_ID, 10, 10), '9999999999') = SA_BILL_CNTRL_EXTRNL_FNX(p_bill_control_seq) 
GROUP BY CaseNumReev, gnc.BILL_CONTROL_SEQ;

在过去的两天里,我完全被自己弄得一团糟。 有了这个,如果你帮我解决问题,我真的很感激。

再次感谢...

1 个答案:

答案 0 :(得分:1)

尝试以下功能,我也假设因为你只传递一个参数,所以剩下的必须是NULL

CREATE OR REPLACE FUNCTION SA_BILL_CNTRL_EXTRNL_FXN(p_client_code IN VARCHAR2 DEFAULT NULL                                               
                                               ,p_bill_seq     IN    VARCHAR2 DEFAULT NULL
                                               ,p_bill_control_seq IN VARCHAR2 DEFAULT NULL)
RETURN VARCHAR2 IS
v_external_id      VARCHAR2(4000);
v_client_code      VARCHAR2(4) := p_client_code;
v_bill_seq         NUMBER := to_number(p_bill_seq);
v_bill_control_seq NUMBER := to_number(p_bill_control_seq);
v_cnt              NUMBER;
BEGIN
     SELECT COUNT(*)
     INTO v_cnt
     FROM BillControl@SQL_SAREP bc
     WHERE bc."ExternalID" IS NOT NULL
     AND bc."ExternalID" != ''
     AND bc."ClientCode" = NVL(p_client_code,bc."ClientCode")
     AND bc."BillControlSeq" = NVL(v_bill_control_seq,bc."BillControlSeq")
     AND bc."BillSeq" = NVL(v_bill_seq,bc."BillSeq");
   IF v_cnt = 1 THEN
      SELECT bc."ExternalID"
      INTO v_external_id
      FROM BillControl@SQL_SAREP bc
      WHERE bc."ExternalID" IS NOT NULL
      AND bc."ExternalID" != ''
      AND bc."ClientCode" = NVL(p_client_code,bc."ClientCode")
      AND bc."BillControlSeq" = NVL(v_bill_control_seq,bc."BillControlSeq")
      AND bc."BillSeq" = NVL(v_bill_seq,bc."BillSeq");
  ELSE
     v_external_id := NULL;
  END IF;
       RETURN v_external_id;
END SA_BILL_CNTRL_EXTRNL_FNX;
/

你的SQL应该

SELECT REC.CaseNumReev,REC.BILL_CONTROL_SEQ FROM
(
 SELECT NVL(TRIM(eb.CASE_NUM_REEVALUATED), TRIM(eb.CASE_NUM_DUPLICATED)) CaseNumReev
  ,gnc.BILL_CONTROL_SEQ AS BILL_CONTROL_SEQ
 FROM EPE_Bill eb ,NETWORK_CROSSWALK gnc
 WHERE substr(eb.MIC_BILL_ID, 5, 4) = SA_BILL_CNTRL_EXTRNL_FNX(p_client_code)
 AND gnc.BILL_CONTROL_SEQ = SA_BILL_CNTRL_EXTRNL_FNX(p_bill_seq) 
 AND (to_number(substr(eb.MIC_BILL_ID, 10, 10), '9999999999')     =SA_BILL_CNTRL_EXTRNL_FNX(p_bill_control_seq))rec    
GROUP BY REC.CaseNumReev,REC.BILL_CONTROL_SEQ