ORA-01008: not all variables bound. They are bound

前端 未结 8 1353
青春惊慌失措
青春惊慌失措 2020-11-29 08:01

I have come across an Oracle problem for which I have so far been unable to find the cause. The query below works in Oracle SQL developer, but when running in .NET it throws

相关标签:
8条回答
  • 2020-11-29 08:28

    On Charles' comment problem: to make things worse, let

    :p1 = 'TRIALDEV'
    

    via a Command Parameter, then execute

    select T.table_name as NAME, COALESCE(C.comments, '===') as DESCRIPTION
    from all_all_tables T
    Inner Join all_tab_comments C on T.owner = C.owner and T.table_name = C.table_name
    where Upper(T.owner)=:p1
    order by T.table_name
    
    558 line(s) affected. Processing time: 00:00:00.6535711
    

    and when changing the literal string from === to ---

    select T.table_name as NAME, COALESCE(C.comments, '---') as DESCRIPTION
    [...from...same-as-above...]
    
    ORA-01008: not all variables bound
    

    Both statements execute fine in SQL Developer. The shortened code:

                Using con = New OracleConnection(cs)
                    con.Open()
                    Using cmd = con.CreateCommand()
                        cmd.CommandText = cmdText
                        cmd.Parameters.Add(pn, OracleDbType.NVarchar2, 250).Value = p
                        Dim tbl = New DataTable
                        Dim da = New OracleDataAdapter(cmd)
                        da.Fill(tbl)
                        Return tbl
                    End Using
                End Using
    

    using Oracle.ManagedDataAccess.dll Version 4.121.2.0 with the default settings in VS2015 on the .Net 4.61 platform.

    So somewhere in the call chain, there might be a parser that is a bit too aggressively looking for one-line-comments started by -- in the commandText. But even if this would be true, the error message "not all variables bound" is at least misleading.

    0 讨论(0)
  • 2020-11-29 08:29

    Came here looking for help as got same error running a statement listed below while going through a Udemy course:

    INSERT INTO departments (department_id, department_name)
                      values( &dpet_id, '&dname');  
    

    I'd been able to run statements with substitution variables before. Comment by Charles Burns about possibility of server reaching some threshold while recreating the variables prompted me to log out and restart the SQL Developer. The statement ran fine after logging back in.

    Thought I'd share for anyone else venturing here with a limited scope issue as mine.

    0 讨论(0)
  • I know this is an old question, but it hasn't been correctly addressed, so I'm answering it for others who may run into this problem.

    By default Oracle's ODP.net binds variables by position, and treats each position as a new variable.

    Treating each copy as a different variable and setting it's value multiple times is a workaround and a pain, as furman87 mentioned, and could lead to bugs, if you are trying to rewrite the query and move things around.

    The correct way is to set the BindByName property of OracleCommand to true as below:

    var cmd = new OracleCommand(cmdtxt, conn);
    cmd.BindByName = true;
    

    You could also create a new class to encapsulate OracleCommand setting the BindByName to true on instantiation, so you don't have to set the value each time. This is discussed in this post

    0 讨论(0)
  • 2020-11-29 08:35

    You have two references to the :lot_priprc binding variable -- while it should require you to only set the variable's value once and bind it in both places, I've had problems where this didn't work and had to treat each copy as a different variable. A pain, but it worked.

    0 讨论(0)
  • 2020-11-29 08:35

    The solution in my situation was similar answer to Charles Burns; and the problem was related to SQL code comments.

    I was building (or updating, rather) an already-functioning SSRS report with Oracle datasource. I added some more parameters to the report, tested it in Visual Studio, it works great, so I deployed it to the report server, and then when the report is executed the report on the server I got the error message:

    "ORA-01008: not all variables bound"

    I tried quite a few different things (TNSNames.ora file installed on the server, Removed single line comments, Validate dataset query mapping). What it came down to was I had to remove a comment block directly after the WHERE keyword. The error message was resolved after moving the comment block after the WHERE CLAUSE conditions. I have other comments in the code also. It was just the one after the WHERE keyword causing the error.

    SQL with error: "ORA-01008: not all variables bound"...

    WHERE
    /*
        OHH.SHIP_DATE BETWEEN TO_DATE('10/1/2018', 'MM/DD/YYYY') AND TO_DATE('10/31/2018', 'MM/DD/YYYY')
        AND OHH.STATUS_CODE<>'DL'
        AND OHH.BILL_COMP_CODE=100
        AND OHH.MASTER_ORDER_NBR IS NULL
    */
    
        OHH.SHIP_DATE BETWEEN :paramStartDate AND :paramEndDate
        AND OHH.STATUS_CODE<>'DL'
        AND OHH.BILL_COMP_CODE IN (:paramCompany)
        AND LOAD.DEPART_FROM_WHSE_CODE IN (:paramWarehouse) 
        AND OHH.MASTER_ORDER_NBR IS NULL
        AND LOAD.CLASS_CODE IN (:paramClassCode) 
        AND CUST.CUST_CODE || '-' || CUST.CUST_SHIPTO_CODE IN (:paramShipto) 
    

    SQL executes successfully on the report server...

    WHERE
        OHH.SHIP_DATE BETWEEN :paramStartDate AND :paramEndDate
        AND OHH.STATUS_CODE<>'DL'
        AND OHH.BILL_COMP_CODE IN (:paramCompany)
        AND LOAD.DEPART_FROM_WHSE_CODE IN (:paramWarehouse) 
        AND OHH.MASTER_ORDER_NBR IS NULL
        AND LOAD.CLASS_CODE IN (:paramClassCode) 
        AND CUST.CUST_CODE || '-' || CUST.CUST_SHIPTO_CODE IN (:paramShipto)   
    /*
        OHH.SHIP_DATE BETWEEN TO_DATE('10/1/2018', 'MM/DD/YYYY') AND TO_DATE('10/31/2018', 'MM/DD/YYYY')
        AND OHH.STATUS_CODE<>'DL'
        AND OHH.BILL_COMP_CODE=100
        AND OHH.MASTER_ORDER_NBR IS NULL
    */
    

    Here is what the dataset parameter mapping screen looks like.

    0 讨论(0)
  • 2020-11-29 08:36

    I found how to run the query without error, but I hesitate to call it a "solution" without really understanding the underlying cause.

    This more closely resembles the beginning of my actual query:

    -- Comment
    -- More comment
    SELECT rf.flowrow, rf.stage, rf.process,
    rf.instr instnum, rf.procedure_id, rtd_history.runtime, rtd_history.waittime
    FROM
    (
        -- Comment at beginning of subquery
        -- These two comment lines are the problem
        SELECT sub2.flowrow, sub2.stage, sub2.process, sub2.instr, sub2.pid
        FROM ( ...
    

    The second set of comments above, at the beginning of the subquery, were the problem. When removed, the query executes. Other comments are fine. This is not a matter of some rogue or missing newline causing the following line to be commented, because the following line is a SELECT. A missing select would yield a different error than "not all variables bound."

    I asked around and found one co-worker who has run into this -- comments causing query failures -- several times. Does anyone know how this can be the cause? It is my understanding that the very first thing a DBMS would do with comments is see if they contain hints, and if not, remove them during parsing. How can an ordinary comment containing no unusual characters (just letters and a period) cause an error? Bizarre.

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