ORA-01008:当它们全部绑定时,并非所有变量都绑定

时间:2012-11-23 07:42:33

标签: asp.net sql oracle

我将Oracle查询放到ASP.Net页面时出现问题,其中C#作为其代码。正如您在标题上看到的那样,当查询连接到页面时,我收到错误 ORA-01008:并非所有变量绑定。但是当我尝试在Oracle SQL Developer中运行查询时,它运行得很好。请帮我解决我的问题。

这是我的ASP.Net代码:

select 
      to_char(eventdate,'MM/DD/YYYY') as eventdate
      , workweek
      , shift
      , testerid
      , to_char(datefrom, 'MM/DD/YYYY HH24:MI:SS') as datefrom
      , to_char(dateto, 'MM/DD/YYYY HH24:MI:SS') as dateto
      , (((dateto - datefrom)*24)*60) as totalminutes
      , status
      , statusreason
      , serviceby
      , starjobreason
      , endjobreason
      , lotno
      , device
      , teststep
      , handlerid
      , handlertype
      , operatorid
      , operatorname
      , comments
      --, vendormodel
      from
      (
       with tbl_details as
         ( select
            *
            from
            (
            select 
            hm.resourceid
            , hm.calendarworkweek as workweek
            , hm.cdoname
            , hm.txndate
            , rsh.newstatusname
            , rsh.newreasonname
            , lead(jsh.joborderid) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as joborderid
            , last_value(weph.paramvalue ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as handlerid
            , last_value(hm.containername ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as lotno
            , last_value(hm.employeeid ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as employeeid
            , last_value(hm.specname ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as specname
            , last_value(p.brandname ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as packagecode
            , last_value(p.packagetype ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as packagetype
            , last_value(p.device ignore nulls) over (partition by hm.resourcename order by hm.resourcename, hm.historymainlineid, hm.txndate) as device
            , hm.comments
            , rd.vendormodel
            from historymainline hm 
            inner join resourcedef rd on hm.resourceid = rd.resourceid
            left join resourcestatushistory rsh on hm.historymainlineid = rsh.historymainlineid
            left join a_wipequipmenthistory weh on hm.containerid = weh.containerid and hm.resourceid = weh.equipmentid
            left join a_jobstarthistory jsh on hm.historymainlineid = jsh.historymainlineid
            left join a_wipequipmentparamshistory weph on weh.wipequipmenthistoryid = weph.wipequipmenthistoryid and weph.paramnamename = 'HANDLERID'
            left join product p on hm.productid = p.productid
            where hm.cdoname in ('TrackInLot','EquipmentSetStatus', 'JobStart') and hm.txndategmt > to_date(:startDate, 'MM/DD/YYYY')) 
            where cdoname = 'EquipmentSetStatus'),  
      tbl_details2 as (
            select 
             case when cast(to_char(nvl(dt.txndate, cldr.starttimestamp),'HH24.MISS') as float) < 6.0
                  then trunc(nvl(dt.txndate, cldr.starttimestamp)) - 1 else trunc(nvl(dt.txndate, cldr.starttimestamp)) end as eventdate
            , nvl(hml.calendarworkweek, cww.workweek) as workweek
            , case when cast(to_char(nvl(dt.txndate, cldr.starttimestamp),'HH24.MISS') as float) >= 6 
                  and  cast(to_char(nvl(dt.txndate, cldr.starttimestamp),'HH24.MISS') as float) < 18 then 'A' else 'B' end as shift
            , r.resourcename as testerid
            , nvl(dt.txndate, cldr.starttimestamp) as datefrom
            , lead(nvl(dt.txndate, cldr.starttimestamp)) over (partition by r.resourcename order by nvl(dt.txndate, cldr.starttimestamp)) as dateto
            , dt.newstatusname as status
            , dt.newreasonname as statusreason
            , af.fullname as serviceby
            , jph.jobreasonname as starjobreason
            , jphe.jobreasonname as endjobreason
            , dt.lotno as lotno
            , dt.device as device
            , dt.specname as teststep
            , dt.handlerid as handlerid
            , r.attr_09 as handlertype
            , ai.employeename as operatorid
            , ai.fullname as operatorname
            , ae.comments
            , r.vendormodel
            from resourcedef r 
              inner join  a_calendarshifts cldr on cldr.starttimestamp > (to_date(:startDate, 'MM/DD/YYYY')) + 6/24 and cldr.starttimestamp < (to_date(:endDate, 'MM/DD/YYYY')) + 6/24
              left join tbl_details dt on r.resourceid = dt.resourceid and dt.txndate between cldr.starttimestamp and cldr.endtimestamp
              inner join a_calendarworkweeks cww on dt.txndate between to_date(to_char(cww.startdate, 'MM/DD/YYYY'),'MM/DD/YYYY') and to_date(to_char(cww.enddate, 'MM/DD/YYYY'), 'MM/DD/YYYY')
              left join a_jobprogresshistory jph on dt.joborderid = jph.joborderid and jph.jobresultname = 'BEGIN' and upper(jph.fromstagename) like '%START%'
              left join a_jobprogresshistory jphe on dt.joborderid = jphe.joborderid and jphe.jobresultname = 'END'
              left join historymainline ae on jph.historymainlineid = ae.historymainlineid
              left join historymainline hml on ae.historymainlineid = hml.historymainlineid
              left join employee af on ae.employeeid = af.employeeid
              left join resourcedef ag on dt.handlerid = ag.resourcename
              left join location ah on r.locationid = ah.locationid
              left join employee ai on dt.employeeid = ai.employeeid
              where r.resourcename like :equipID and ah.locationname like '%' and r.attr_02 = 'TESTER'
              order by dt.txndate),

      tbl_details3 as (
              select 
                row_number() over (partition by aa.testerid order by aa.testerid, aa.eventdate, aa.datefrom desc) as numdata
                , row_number() over (partition by aa.testerid, aa.eventdate, aa.shift order by aa.testerid, aa.eventdate, aa.datefrom desc) as numdesc                
                , aa.eventdate
                , aa.workweek
                , aa.shift
                , aa.testerid
                , aa.datefrom
                , aa.dateto
                , aa.status
                , aa.statusreason
                , aa.serviceby
                , aa.starjobreason
                , aa.endjobreason
                , aa.lotno
                , aa.device
                , aa.teststep
                , aa.handlerid
                , aa.handlertype
                , aa.operatorid
                , aa.operatorname
                , aa.comments
                , aa.vendormodel
              from tbl_details2 aa) ,
        tbl_equipfirstdata as (
              select 
                row_number() over (partition by aa.testerid order by aa.testerid, aa.datefrom desc) as numdatdesc
                , row_number() over (partition by aa.testerid, aa.eventdate, aa.shift order by aa.testerid, aa.eventdate, aa.datefrom) as num                
                , aa.eventdate
                , aa.workweek
                , aa.shift
                , aa.testerid
                , aa.datefrom
                , aa.dateto
                , aa.status
                , aa.statusreason
                , aa.serviceby
                , aa.starjobreason
                , aa.endjobreason
                , aa.lotno
                , aa.device
                , aa.teststep
                , aa.handlerid
                , aa.handlertype
                , aa.operatorid
                , aa.operatorname
                , aa.comments
                , aa.vendormodel
              from tbl_details2 aa
              )

      select 
                ba.eventdate
                , ba.workweek
                , ba.shift
                , ba.testerid
                , ba.datefrom
                , (case when ba.shift = 'A' and ba.numdesc = 1 then trunc(ba.eventdate) + 18/24 
                       when ba.shift = 'B' and ba.numdesc = 1  then trunc(ba.eventdate + 1) + 6/24 else ba.dateto end) as dateto
                , ba.status
                , ba.statusreason
                , ba.serviceby
                , ba.starjobreason
                , ba.endjobreason
                , ba.lotno
                , ba.device
                , ba.teststep
                , ba.handlerid
                , ba.handlertype
                , ba.operatorid
                , ba.operatorname
                , ba.comments
                , ba.vendormodel
        from tbl_details3 ba
    union all
             select 
                 case when ba.shift = 'B' then ba.eventdate + 1 else ba.eventdate end as eventdate
                , ba.workweek
                , case when ba.shift = 'B' then 'A'
                      when ba.shift = 'A' and ba.dateto > (trunc(ba.eventdate) + 18/24) then 'B' else ba.shift end as shift
                , ba.testerid
                , case when ba.shift = 'B' then (trunc(ba.eventdate + 1) + 6/24)
                     when ba.shift = 'A' and ba.dateto > (trunc(ba.eventdate) + 18/24) then (trunc(ba.eventdate) + 18/24) else ba.datefrom end as datefrom
                , case when ba.shift = 'B' and ba.dateto > (trunc(ba.eventdate + 1) + 18/24)  then (trunc(ba.eventdate + 1) + 18/24) else ba.dateto end as dateto
                , ba.status
                , ba.statusreason
                , ba.serviceby
                , ba.starjobreason
                , ba.endjobreason
                , ba.lotno
                , ba.device
                , ba.teststep
                , ba.handlerid
                , ba.handlertype
                , ba.operatorid
                , ba.operatorname
                , ba.comments
                , ba.vendormodel
        from tbl_details3 ba
            where ba.numdesc = 1 and cast(to_char(ba.datefrom,'HH24.MISS') as float) <> 6.0 
                  and cast(to_char(ba.datefrom,'HH24.MISS') as float) <> 18.0 
                  and cast(to_char(ba.dateto,'HH24.MISS') as float) <> 18.0
                  and ba.shift = 'A'                  
   union all
             select 
                 case when ba.shift = 'B' then eventdate + 1 else ba.eventdate end as eventdate
                , ba.workweek
                , case when ba.shift = 'B' then 'A' else ba.shift end as shift
                , ba.testerid
                , case when ba.shift = 'B' then (trunc(ba.eventdate + 1) + 6/24)  else datefrom end as datefrom
                , case when ba.shift = 'B' and ba.dateto > (trunc(ba.eventdate + 1) + 18/24) then (trunc(ba.eventdate + 1) + 18/24) else ba.dateto end as dateto
                , ba.status
                , ba.statusreason
                , ba.serviceby
                , ba.starjobreason
                , ba.endjobreason
                , ba.lotno
                , ba.device
                , ba.teststep
                , ba.handlerid
                , ba.handlertype
                , ba.operatorid
                , ba.operatorname
                , ba.comments
                , ba.vendormodel
        from tbl_details3 ba
            where ba.numdesc = 1  and cast(to_char(ba.datefrom,'HH24.MISS') as float) <> 6.0 and ba.shift = 'B' and cast(to_char(ba.dateto,'HH24.MISS') as float) <> 6.0
     union all
             select 
                ba.eventdate
                , ba.workweek
                , ba.shift
                , ba.testerid
                , (case when ba.shift = 'A' then trunc(ba.eventdate) + 6/24 
                        when ba.shift = 'B'  then trunc(ba.eventdate) + 18/24 else ba.datefrom  end) as datefrom
                ,(case when ba.shift = 'B'  then ba.datefrom else ba.dateto  end) as dateto
                , ba.status
                , ba.statusreason
                , ba.serviceby
                , ba.starjobreason
                , ba.endjobreason
                , ba.lotno
                , ba.device
                , ba.teststep
                , ba.handlerid
                , ba.handlertype
                , ba.operatorid
                , ba.operatorname
                , ba.comments
                , ba.vendormodel
        from tbl_details3 ba
    where ba.numdata = 1 and cast(to_char(datefrom,'HH24.MISS') as float) <> 6.0 and cast(to_char(datefrom,'HH24.MISS') as float) <> 18.0 and ba.shift = 'B' and cast(to_char(dateto,'HH24.MISS') as float) <> 18.0)
where eventdate BETWEEN ((TRUNC(to_date(:startDate, 'MM/DD/YYYY'))) + 6/24) AND (trunc(to_date(:endDate, 'MM/DD/YYYY')) + 6/24) and teststep is not null and testerid like :equipID and vendormodel like :venmod and handlertype like :handType or handlerid like :handID or operatorid like :operID and handlerid is not null --and rownum > 1
order by testerid, eventdate, datefrom

以下是我的代码:

protected void btnFilter_Click(object sender, EventArgs e)
        {
            Session["startDate"] = string.Empty;
            Session["endDate"] = string.Empty;
            Session["equipID"] = string.Empty;
            Session["venmod"] = string.Empty;
            Session["handID"] = string.Empty;
            Session["handType"] = string.Empty;
            Session["operID"] = string.Empty;

            if (String.IsNullOrEmpty(txtDateFrom.Text))
            {
                Session["startDate"] = DateTime.Now.AddDays(-1).Date.ToShortDateString();
                txtDateFrom.Text = DateTime.Now.AddDays(-1).Date.ToShortDateString();
            }
            else
            {
                Session["startDate"] = txtDateFrom.Text.ToString();
            }

            if (String.IsNullOrEmpty(txtDateTo.Text))
            {
                Session["endDate"] = DateTime.Now.Date.ToShortDateString();
                txtDateTo.Text = DateTime.Now.Date.ToShortDateString();
            }
            else
            {
                Session["endDate"] = txtDateTo.Text.ToString();
            }

            if (ddlTesterID.SelectedItem.Value.ToString() == "")
            {
                Session["equipID"] = "%".ToString();
            }
            else
            {
                Session["equipID"] = ddlTesterID.SelectedItem.Value.ToString();
            }

            if (ddlTesterType.SelectedItem.Value.ToString() == "")
            {
                Session["venmod"] = "%".ToString();
            }
            else
            {
                Session["venmod"] = ddlTesterType.SelectedItem.Value.ToString();
            }

            if (ddlHandlerID.SelectedItem.Value.ToString() == "")
            {
                Session["handID"] = "%".ToString();
            }
            else
            {
                Session["handID"] = ddlHandlerID.SelectedItem.Value.ToString();
            }

            if (ddlHandlerType.SelectedItem.Value.ToString() == "")
            {
                Session["handType"] = "%".ToString();
            }
            else
            {
                Session["handType"] = ddlHandlerType.SelectedItem.Value.ToString();
            }

            if (String.IsNullOrEmpty(txtOperatorID.Text))
            {
                Session["operID"] = "%".ToString();
            }
            else
            {
                Session["operID"] = txtOperatorID.Text.ToString();
            }

            //X.MessageBox.Alert("Warning", Session["venmod"].ToString()).Show();

            UltraWebGrid1.DataSourceID = EDTDS.ID;
            UltraWebGrid1.DataBind();
        }

这是SqlDataSource:

<asp:SqlDataSource ID="EDTDS" runat="server" 
           ConnectionString="<%$ ConnectionStrings:Camstar %>" 
            ProviderName="<%$ ConnectionStrings:Camstar.ProviderName %>"  SelectCommand="select ..."
            <SelectParameters>
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="startDate" 
                    SessionField="startDate" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="endDate" 
                    SessionField="endDate" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="equipID" 
                    SessionField="equipID" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="venmod" 
                    SessionField="venmod" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="handType" 
                    SessionField="handType" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="handID" 
                    SessionField="handID" />
                <asp:SessionParameter ConvertEmptyStringToNull="False" Name="operID" 
                    SessionField="operID" />
            </SelectParameters>
        </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

在ODP.NET的情况下经常会发生此错误。问题在于OracleCommand在将查询参数与提供的参数匹配时的默认行为。默认情况下,使用 position 方法代替 name 方法。实际上,无论名称如何,添加到命令的每个参数都将被视为原样,如果在查询文本中引用参数两次,则必须为其添加两次值!

可以通过在BindByName上将true设置为OracleCommad来覆盖此默认行为,但如果SqlDataSource您无权访问它,则需要以正确的顺序提供多个值的时间:

<asp:SqlDataSource ID="EDTDS" runat="server" ConnectionString="<%$ ConnectionStrings:Camstar %>" ProviderName="<%$ ConnectionStrings:Camstar.ProviderName %>" SelectCommand="select ..."
    <SelectParameters>
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="startDate" SessionField="startDate" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="startDate" SessionField="startDate" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="endDate"  SessionField="endDate" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="equipID" SessionField="equipID" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="startDate" SessionField="startDate" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="endDate"  SessionField="endDate" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="equipID" SessionField="equipID" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="venmod" SessionField="venmod" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="handType" SessionField="handType" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="handID" SessionField="handID" />
        <asp:SessionParameter ConvertEmptyStringToNull="False" Name="operID" SessionField="operID" />
    </SelectParameters>
</asp:SqlDataSource>

(我希望我没有错过任何一个)

作为替代方案,您可以考虑创建自己的从ODP.NET派生的提供商,其中BindByName已调整。