ORA-01036: illegal variable name/number when running query through C#

前端 未结 10 1646
执念已碎
执念已碎 2020-12-06 17:20

I am trying to use ALTER USER query for Oracle database using OracleCommand in C# in the following code. It creates the query if the values for Username and pas

相关标签:
10条回答
  • 2020-12-06 17:50
     cmd.Parameters.Add(new OracleParameter("GUSERID ", OracleType.VarChar)).Value = userId;
    

    I was having eight parameters and one was with space at the end as shown in the above code for "GUSERID ".Removed the space and everything started working .

    0 讨论(0)
  • 2020-12-06 17:56

    The root cause

    In Oracle you have three kinds of SQL statements (and additionally there are PL/SQL blocks):

    • Statements in the Data Definiton Language (DDL). These statements modify the structure of the database. They begin usually with the verbs "ALTER" or "CREATE"
    • Statements in the Data Modification Langugage (DML). There statements modify the content inside of tables, leaving the structure of each table unmodified. These statements usually begin with "INSERT", "MERGE" or "DELETE".
    • Statements in what I call "query language" (there seems to be no canonical name for these). This statements start with the verb "SELECT".

    Bind variables in Oracle are only allowed in some special places in DML and query statements. You are trying to use bind variables in a places where they are not allowed. Hence the error.

    Solution

    Build your statement without bind variables. Build the complete query string instead using string concatenation.

    If you want to sanitize the input before concatenating the string, use the DBMS_ASSERT package.

    Background

    Bind variables can only be used when Oracle can build a query plan without knowing the value of the variable. For DDL statements, there is no query plan. Hence bind variables are not allowed.

    In DML and query statements, bind variables are only allowed, when they are used inside a tuple (regarding the underlying set theory), i.e. when the value will be compared with the value in a table or when the value will be inserted in a table. They are not allowed to change the structure of the execution plan (e.g. to change the target table or to change the number of comparisons).

    0 讨论(0)
  • 2020-12-06 17:59

    Just for others getting this error and looking for info on it, it is also thrown if you happen to pass a binding parameter and then never use it. I couldn't really find that stated clearly anywhere but had to prove it through trial and error.

    0 讨论(0)
  • The Oracle error ORA-01036 means that the query uses an undefined variable somewhere. From the query we can determine which variables are in use, namely all that start with @. However, if you're inputting this into an advanced query, it's important to confirm that all variables have a matching input parameter, including the same case as in the variable name, if your Oracle database is Case Sensitive.

    0 讨论(0)
  • 2020-12-06 18:06

    I was having the same problem in an application that I was maintaining, among all the adjustments to prepare the environment, I also spent almost an hour banging my head with this error "ORA-01036: illegal variable name / number" until I found out that the application connection was pointed to an outdated database, so the application passed two more parameters to the outdated database procedure causing the error.

    0 讨论(0)
  • 2020-12-06 18:06

    You cannot pass user/table name to pl/sql with a parameter. You can create a procedure and build sql and then execute immediately to achieve that.

    0 讨论(0)
提交回复
热议问题