0

以下是我尝试使用 oracle 连接字符串在我的 C# 代码中运行的命令,一切都很好(相同的查询在 toad 中运行顺利)。然而,在填充数据集时,它给出了 ORA-00911 的错误:无效字符。我不知道为什么,请帮忙。

 OracleCommand cmd = new OracleCommand(
            "SELECT DSAT.ORGANIZATION_ID, APPS.DAW_INV_ORG_NAME(DSAT.ORGANIZATION_ID) ORGANIZATION_NAME,APPS.DAW_CAT_NAME(MC.SEGMENT4) PRODUCT,TRUNC(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE)) TRANSACTION_DATE,"
            +"TO_CHAR(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE),'MM-YYYY') MONTH,DSAT.ITEM_SEGMENTS ITEM_CODE, DSAT.ITEM_DESCRIPTION,"
            +"DSAT.CURRENT_FORECAST_QUANTITY PLAN_QTY FROM APPS.DAW_SCPDB_ASCP_TPP_V   DSAT,APPS.MTL_ITEM_CATEGORIES   MIC,"
            +"APPS.MTL_CATEGORIES      MC"
        + " WHERE NVL(SCH_END_DATE,SCH_START_DATE) BETWEEN TO_DATE(@vfrom,'dd/mm/yyyy') AND TO_DATE(@vto,'dd/mm/yyyy')"+
            " AND  MIC.CATEGORY_SET_ID=1100000061 AND  MC.CATEGORY_ID=MIC.CATEGORY_ID AND  DSAT.INVENTORY_ITEM_ID=MIC.INVENTORY_ITEM_ID"
            +" AND  DSAT.ORGANIZATION_ID=MIC.ORGANIZATION_ID"
            +" AND DSAT.FORECAST_SET IN (@vForecastSetDPL1,@vForecastSetDPL2,@vForecastSetURIL)"
            +" AND APPS.DAW_CAT_NAME(MC.SEGMENT4) ='MICRO WAVE OVEN';", con);
        cmd.Parameters.Add("@vfrom",OracleDbType.Varchar2).Value= vfrom;
        cmd.Parameters.Add("@vto",OracleDbType.Varchar2).Value= vto;
        cmd.Parameters.Add("@vForecastSetDPL1", OracleDbType.Varchar2).Value=vForecastSetDPL1;
        cmd.Parameters.Add("@vForecastSetDPL2",OracleDbType.Varchar2).Value=vForecastSetDPL2;
        cmd.Parameters.Add("@vForecastSetURIL",OracleDbType.Varchar2).Value= vForecastSetURIL;

        cmd.CommandType = CommandType.Text;

使用的所有变量都是字符串类型,因此对于日期,我必须使用 TO_DATE。其余参数应该是字符串类型。

4

2 回答 2

1

您的问题是在您的查询中使用“@”,但对于 Oracle,您需要使用“:”

 BETWEEN TO_DATE(@vfrom,'dd/mm/yyyy') - incorrect 
 BETWEEN TO_DATE(:vfrom,'dd/mm/yyyy') - correct

要使用存储过程,您需要创建返回 sys_refcursor 的输出参数,一切都应该没问题

于 2013-08-30T18:22:59.310 回答
1

Instead of sending parameters we can build the query first and then execute it. In your code we don't need semicolon in 'MICRO WAVE OVEN';"

    string _currentQuery = "SELECT DSAT.ORGANIZATION_ID, APPS.DAW_INV_ORG_NAME(DSAT.ORGANIZATION_ID) ORGANIZATION_NAME,APPS.DAW_CAT_NAME(MC.SEGMENT4) PRODUCT,TRUNC(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE)) TRANSACTION_DATE,"
    +"TO_CHAR(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE),'MM-YYYY') MONTH,DSAT.ITEM_SEGMENTS ITEM_CODE, DSAT.ITEM_DESCRIPTION,"
    +"DSAT.CURRENT_FORECAST_QUANTITY PLAN_QTY FROM APPS.DAW_SCPDB_ASCP_TPP_V DSAT,APPS.MTL_ITEM_CATEGORIES MIC,"
    +"APPS.MTL_CATEGORIES MC"
+ " WHERE NVL(SCH_END_DATE,SCH_START_DATE) BETWEEN TO_DATE('" + vfrom + "','dd/mm/yyyy') AND TO_DATE('" + vto + "','dd/mm/yyyy')" +
    " AND  MIC.CATEGORY_SET_ID=1100000061 AND  MC.CATEGORY_ID=MIC.CATEGORY_ID AND  DSAT.INVENTORY_ITEM_ID=MIC.INVENTORY_ITEM_ID"
    +" AND  DSAT.ORGANIZATION_ID=MIC.ORGANIZATION_ID"
    +" AND DSAT.FORECAST_SET IN ('" + vForecastSetDPL1 + "','" + vForecastSetDPL2 + "','" + vForecastSetURIL + "')"
    +" AND APPS.DAW_CAT_NAME(MC.SEGMENT4) ='MICRO WAVE OVEN'";

    OracleCommand cmd = new OracleCommand(_currentQuery,con);

    cmd.CommandType = CommandType.Text;

EDIT

Changes to your code. I have not tested with real data

OracleCommand cmd = new OracleCommand(
            "SELECT DSAT.ORGANIZATION_ID, APPS.DAW_INV_ORG_NAME(DSAT.ORGANIZATION_ID) ORGANIZATION_NAME,APPS.DAW_CAT_NAME(MC.SEGMENT4) PRODUCT,TRUNC(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE)) TRANSACTION_DATE,"
            +"TO_CHAR(NVL(DSAT.SCH_END_DATE,DSAT.SCH_START_DATE),'MM-YYYY') MONTH,DSAT.ITEM_SEGMENTS ITEM_CODE, DSAT.ITEM_DESCRIPTION,"
            +"DSAT.CURRENT_FORECAST_QUANTITY PLAN_QTY FROM APPS.DAW_SCPDB_ASCP_TPP_V   DSAT,APPS.MTL_ITEM_CATEGORIES   MIC,"
            +"APPS.MTL_CATEGORIES      MC"
        + " WHERE NVL(SCH_END_DATE,SCH_START_DATE) BETWEEN TO_DATE(:vfrom,'dd/mm/yyyy') AND TO_DATE(:vto,'dd/mm/yyyy')"+
            " AND  MIC.CATEGORY_SET_ID=1100000061 AND  MC.CATEGORY_ID=MIC.CATEGORY_ID AND  DSAT.INVENTORY_ITEM_ID=MIC.INVENTORY_ITEM_ID"
            +" AND  DSAT.ORGANIZATION_ID=MIC.ORGANIZATION_ID"
            +" AND DSAT.FORECAST_SET IN (:vForecastSetDPL1,:vForecastSetDPL2,:vForecastSetURIL)"
            +" AND APPS.DAW_CAT_NAME(MC.SEGMENT4) ='MICRO WAVE OVEN'", con);
        cmd.Parameters.Add("vfrom",OracleDbType.Varchar2).Value= vfrom;
        cmd.Parameters.Add("vto",OracleDbType.Varchar2).Value= vto;
        cmd.Parameters.Add("vForecastSetDPL1", OracleDbType.Varchar2).Value=vForecastSetDPL1;
        cmd.Parameters.Add("vForecastSetDPL2",OracleDbType.Varchar2).Value=vForecastSetDPL2;
        cmd.Parameters.Add("vForecastSetURIL",OracleDbType.Varchar2).Value= vForecastSetURIL;

        cmd.CommandType = CommandType.Text;

post errors if these solutions doesnt work for you

于 2013-08-30T15:53:34.073 回答