Search code examples
c#sqloracle-databaseprocedure

Oracle stored procedure creation from C# code fails for PL/SQL developer


I tried to create a SQL procedure on an Oracle server. Here is my code,

private const string InsertCallProc =
        @"CREATE OR REPLACE PROCEDURE INSERTCALL (Id in varchar2,No in varchar2,
           Surname in varchar2, Name in varchar2, CallType in varchar2,
           CallDate in date, CallNo in varchar2, VoiceNetwork in varchar2,
           Type in varchar2,TalkTime in number,PriceWithDiscount in number,
           PriceWithoutDiscount in number, LmTime in date)
        AS
            BEGIN
                INSERT INTO TMOBILE_R_CALLS (ID,NO,SURNAME,NAME,CALL_TYPE,CALL_DATE,CALL_NO, 
                    VOICE_NETWORK, TYPE,TALK_TIME, PRICE_WITH_DISCOUNT, PRICE_WITHOUT_DISCOUNT,LM_TIME)
                VALUES(Id, No, Surname, Name, CallType,CallDate, CallNo, VoiceNetwork,Type, TalkTime, 
                    PriceWithDiscount,PriceWithoutDiscount, LmTime);
    END;";

Method with execute this sql cmd, input parameter is InsertCallProc field.

    private void ExecuteNonQueryCmd(string cmdText)
    {
        using (var conn = new OracleConnection(GenerateConnectionString()))
        {
            conn.Open();

            var cmd = new OracleCommand
            {
                Connection = conn,
                CommandText = cmdText
            };

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

If I called this method, the procedure is created. But if I check the procedure in PL/SQL developer I see this:

Compilation errors for PROCEDURE SYSTEM.INSERTCALL

Error: PLS-00103: Encountered the symbol "" when expecting one of the following:

current Line: 1 Text: CREATE OR REPLACE PROCEDURE INSERTCALL (Id in varchar2,No in varchar2,

I think this is caused by formatting the string in C# code. Because if I execute the same SQL code on Oracle server procedure is created without problems.

What is a good solution for this problem?


Solution

  • Perhaps it's the newlines embedded in your C# string. The error

    Encountered the symbol "" when expecting one of the following

    indicates to me that there's a special character that's not printable being encountered. It's also indicating that the problem is in the first line.

    Also, it's generally a Bad Idea to create objects in the SYSTEM or SYS schemas.

    As for the answers pointing the finger at reserved word use: while it's not a very good practice, it is likely not the culprit here:

    SQL> CREATE OR REPLACE PROCEDURE p(TYPE IN VARCHAR2) IS
      2  BEGIN
      3    dbms_output.put_line(TYPE);
      4  END;
      5  /
    
    Procedure created
    SQL> DECLARE
      2  BEGIN
      3    p('This is a test');
      4  END;
      5  /
    
    This is a test
    
    PL/SQL procedure successfully completed
    
    SQL>