Oracle-IF声明到WHERE子句中

时间:2019-04-10 15:46:46

标签: c# oracle if-statement

我应该将C#代码转换为Oracle中的存储过程。

我写了一个存储过程,其中的情况是在where但没有返回正确的数据。我在哪里错了?

问题在于管理输入参数的IF。

Oracle:

CREATE OR REPLACE PROCEDURE SP_S_RICCFEMESSE 
(
    NUMEROCV        IN VARCHAR2,
    PROFILO         IN NUMBER,
    SALAES          IN NUMBER,
    DATAINIZIO      IN VARCHAR2,
    DATAFINE        IN VARCHAR2,
    ric_carteemesse OUT SYS_REFCURSOR
)
AS 
BEGIN
    OPEN ric_carteemesse FOR
       SELECT DISTINCT A.*
                ,DESCARDPRFCFR AS TIPO_CARTA
                ,TO_CHAR(DATNAS,'DD/MM/YYYY') AS DATA_DI_NASCITA 
                ,TO_CHAR(A.DATIVOPSA,'DD/MM/YYYY HH24:MI:SS') AS DATA_EMISSIONE 
                ,TO_CHAR(DATINIVALRTA,'DD/MM/YYYY') AS INIZIO_VALIDITA_CARTA 
                ,TO_CHAR(DATFINIVALRTA,'DD/MM/YYYY') AS FINE_VALIDITA_CARTA 
                ,C.S_DENOMINAZIONE AS SALA_ES 
                ,CASE 
                    WHEN NVL(SCDUP,0) = '0' 
                    THEN 'NO' 
                    WHEN SCDUP > 0 
                    THEN 'SI' 
                END AS DUPLICATO 
FROM 
DEMIRTAFRC A 
INNER JOIN SMART_CARD B 
    ON A.CODSRE = B.COD_SM 
LEFT JOIN RIVENDITA C 
    ON B.S_POSTAZIONE_UM = TO_CHAR(C.COD_RIVENDITA) 
INNER JOIN DANACARDPRFCFR D ON A.CODPRF= D.CARDPRFCFR 
WHERE 
    (B.D_ELETTRIFICAZIONE BETWEEN TO_DATE(DATAINIZIO,'DD/MM/YYYY') AND TO_DATE(DATAFINE,'DD/MM/YYYY') 
    AND A.MDLCSG = 'CBM' AND CODSRE IS NOT NULL)
    AND NUMEROCV = (CASE WHEN NUMEROCV <> '' 
                         THEN A.CODRTA
                         ELSE null 
                    END)
    AND PROFILO = (CASE WHEN PROFILO <> 0 
                        THEN B.COD_REQUISITI
                        ELSE null 
                   END)
    AND SALAES = (CASE WHEN SALAES <> 999
                       THEN B.S_POSTAZIONE_UM
                       ELSE null 
                  END)
ORDER BY A.DATETR DESC;

END SP_S_RICCFEMESSE;

这是要替换的C#代码:

if (_NumCV.ToString().Trim() != "")
                _cmd.CommandText += " AND A.CODRTA = '" + _NumCV + "' ";

            if (_Profilo != 0)
                _cmd.CommandText += " AND B.COD_REQUISITI = " + _Profilo;

            if (_SalaES != 999)
                _cmd.CommandText += " AND B.S_POSTAZIONE_UM = '" + _SalaES + "' ";

3 个答案:

答案 0 :(得分:0)

您在PROFILO,SALAES,NUMEROCV列中是否具有空值? 如果是,则必须在此列中的何处设置nvl。

因为您c#说您可能为空,请尝试以下操作:

 CREATE OR REPLACE PROCEDURE SP_S_RICCFEMESSE 
(
    NUMEROCV        IN VARCHAR2,
    PROFILO         IN NUMBER,
    SALAES          IN NUMBER,
    DATAINIZIO      IN VARCHAR2,
    DATAFINE        IN VARCHAR2,
    ric_carteemesse OUT SYS_REFCURSOR
)


AS 
BEGIN

OPEN ric_carteemesse FOR
SELECT DISTINCT A.*
                ,DESCARDPRFCFR AS TIPO_CARTA
                ,TO_CHAR(DATNAS,'DD/MM/YYYY') AS DATA_DI_NASCITA 
                ,TO_CHAR(A.DATIVOPSA,'DD/MM/YYYY HH24:MI:SS') AS DATA_EMISSIONE 
                ,TO_CHAR(DATINIVALRTA,'DD/MM/YYYY') AS INIZIO_VALIDITA_CARTA 
                ,TO_CHAR(DATFINIVALRTA,'DD/MM/YYYY') AS FINE_VALIDITA_CARTA 
                ,C.S_DENOMINAZIONE AS SALA_ES 
                ,CASE 
                    WHEN NVL(SCDUP,0) = '0' 
                    THEN 'NO' 
                    WHEN SCDUP > 0 
                    THEN 'SI' 
                END AS DUPLICATO 
FROM 
DEMIRTAFRC A 
INNER JOIN SMART_CARD B 
    ON A.CODSRE = B.COD_SM 
LEFT JOIN RIVENDITA C 
    ON B.S_POSTAZIONE_UM = TO_CHAR(C.COD_RIVENDITA) 
INNER JOIN DANACARDPRFCFR D ON A.CODPRF= D.CARDPRFCFR 
WHERE 
    (B.D_ELETTRIFICAZIONE BETWEEN TO_DATE(DATAINIZIO,'DD/MM/YYYY') AND TO_DATE(DATAFINE,'DD/MM/YYYY') 
    AND A.MDLCSG = 'CBM' AND CODSRE IS NOT NULL)
    AND nvl(NUMEROCV, ' ') = (CASE WHEN nvl(NUMEROCV, ' ') <> ' ' 
                         THEN A.CODRTA
                         ELSE null 
                    END)
    AND nvl(PROFILO, 0) = (CASE WHEN nvl(PROFILO, 0) <> 0 
                        THEN B.COD_REQUISITI
                        ELSE null 
                   END)
    AND NVL(SALAES, 0) = (CASE WHEN NVL(SALAES, 0) <> 999
                       THEN B.S_POSTAZIONE_UM
                       ELSE null 
                  END)
ORDER BY A.DATETR DESC;

END SP_S_RICCFEMESSE;

答案 1 :(得分:0)

NULL永远不等于NULL。因此,如果任何CASE语句出现在ELSE分支下,则该语句将评估为FALSE。另外,在Oracle中,空字符串与NULL相同(是的,这是有争议的,但是Oracle已经使用了近40年了,它就是这样),这意味着它将始终为FALSE:NUMEROCV <> ''。 / p>

简单的解决方案是,不要在WHERE子句中使用CASE。这要清楚得多:

AND (NUMEROCV is null or NUMEROCV = A.CODRTA)
AND (PROFILO = 0 or PROFILO = B.COD_REQUISITI)
AND (SALAES = 999 or SALAES = B.S_POSTAZIONE_UM))

答案 2 :(得分:0)

如果您有很多条件,则可以动态构建sql

声明sql_statement

sql_statement VARCHAR2(...);

sql_statement := 'SELECT .... WHERE ' // your select

IF PROFILO <> 0 THEN // your criteria here
    sql_statement := sql_statement || ' AND B.COD_REQUISITI = ' || PROFILO // your criteria here;

EXECUTE IMMEDIATE(sql_statement);