2015-02-03 6 views
0

Oracle DB에서 데이터를 수집하는 데 어려움을 겪고 있습니다. 연결이 끊어지기 때문에 문제는 쿼리 내에 있습니다. 현재 쿼리는 아무것도 반환하지 않으며 VBA도 그것에 대해 불평하지 않습니다. 여기 VBA - 여러 AND 문이 작동하지 않는 쿼리

코드입니다 :

Sub Connect_XXXX() 

Dim conn As ADODB.Connection 
Set conn = New ADODB.Connection 
Dim rs As New ADODB.Recordset 
Set rs = New ADODB.Recordset 
Dim myQuery As ADODB.Command 
Set myQuery = New ADODB.Command 


conn.Open "Provider=OraOLEDB.Oracle;" & _ 
      "Data Source=XXXX;" & _ 
      "User Id=YYYY;" & _ 
      "Password=ZZZZ" 

myQuery.ActiveConnection = conn 

myQuery.CommandText = "SELECT sta.index_id, sta.index_action as Action, sta.ticker, sta.company, sta.announcement_date as A_Date, sta.announcement_time as A_Time, " & _ 
         "sta.effective_date as E_Date, dyn.index_supply_demand as BS_Shares, dyn.net_index_supply_demand as Net_BS_Shares, dyn.est_funding_trade as BS_Value, " & _ 
         "dyn.trade_adv_perc/100 as Days_to_Trade, dyn.pre_index_weight/100 as Wgt_Old, dyn.post_index_weight/100 as Wgt_New, dyn.net_index_weight/100 as Wgt_Chg, " & _ 
         "dyn.pre_est_index_holdings as Index_Hldgs_Old, dyn.post_est_index_holdings as Index_Hldgs_New, dyn.net_est_index_holdings as Index_Hldgs_Chg, sta.index_action_details as Details " & _ 
         "FROM index_analysis.eq_index_actions_dyn dyn, index_analysis.eq_index_actions_static sta " & _ 
         "WHERE (sta.action_id = dyn.action_id) AND (sta.announcement_date=dyn.price_date) AND (sta.announcement_date >= '01-January-2015') AND (sta.announcement_date <= '30-January-2015') " & _ 
         "ORDER by sta.index_id,sta.announcement_date" 



Set rs = myQuery.Execute 

Sheets("Sheet1").Range("A1").CopyFromRecordset rs 

rs.Close 
conn.Close 

End Sub 

내가 쿼리 많은 주위를 연주했습니다, 그리고 나는 WHERE 후와 문장의 일부를 제거하고 더 적은으로 시도하여 어떤 결과를 얻을 수있었습니다 필드는 SELECT 문에 있지만이 결과가 나에게 잘 작동하려면 모두 필요합니다. 이상한 점은 Oracle Sql Developer (DB에 연결 한 후)와 같은 문제에서 동일한 쿼리를 실행하면 원하는 결과가 표시된다는 것입니다. 정말 도움이 필요해, 고마워!

답변

2

announcement_datedate이라고 가정하면 날짜 범위를 하드 코딩 할 경우 문자열이 아닌 날짜와 비교할 수 있습니다. 당신은 날짜로

sta.announcement_date >= to_date('01-January-2015', 'DD-Month-YYYY') 

를 문자열로 변환하는 명시 적 형식 마스크 to_date를 사용하거나 항상 형식이 문자 그대로의 날짜 사용할 수 있습니다 YYYY를-MM-DD

sta.announcement_date >= date '2015-01-01' 

내 생각 엔 SQL Developer에서 NLS_DATE_FORMAT을 'DD-Month-YYYY'로 구성했기 때문에 코드가 SQL Developer에서 작동하는 것입니다.

사실 실제로 날짜 범위와 같은 항목을 하드 코딩하는 대신 바인드 변수를 사용해야합니다. 날짜 값을 바인드한다고 가정하면 문자열에서 날짜로의 변환 (있는 경우)은 VB에서 발생하며 세션의 NLS 설정에 의존하지 않습니다. 바인드 변수를 선호하는 성능 및 보안상의 이유도 있습니다.

관련 문제