0

I am trying to pass PL/SQL query from file via VBA to the Oracle DB.

Simple test query like SELECT * FROM DUAL works flawlessly.

However just a bit more complicated (even not yet PL/SQL) like

SELECT x, y, z FROM table WHERE DATE = '5may2017'

already fails (actually no explicit error, just empty result).

I'm connecting to Conn.Provider = "OraOLEDB.Oracle.1", apply user schema by ALTER SESSION SET CURRENT_SCHEMA=MyUserSchema. Finally my last code-snippet is:

With Cmd
    .Properties("PLSQLRSet") = True '(actually this line doesnt affect)'
    .ActiveConnection = Conn
    .CommandType = adCmdText
    .CommandText = SqlStatement
    Set rs = .Execute
End With

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

So, excel sheet is populated with the 1st test simpliest query, however remains empty for the more complex part (I have no semicolon in the end :) ) However final sql-script will be 400+ lines long...

What am I missing at this step?

6
  • PS. I don't have any procedures in the DB, and the access is ReadOnly, so I'm not able to run PL/SQL from procedures Commented May 22, 2017 at 14:51
  • 1
    PLease show the table definition. No data is not a failure. Commented May 22, 2017 at 14:57
  • 1
    Quick look, if you are not getting an error and are getting an empty result set, that probably means the query worked and found nothing that met your specifications. Possibly you have no records with that date, or you specified the date in a format that it did not recognize. What happens if you run it without the WHERE clause? Commented May 22, 2017 at 14:57
  • 1
    For Toad users, check your NLS SELECT * FROM NLS_SESSION_PARAMETERS; ; your NLS_DATE_FORMAT is not default one DD-MON-RR. Commented May 22, 2017 at 16:09
  • I think it must be Set rs = .Open instead of Set rs = .Execute Commented May 22, 2017 at 16:15

1 Answer 1

1

You might try instead ANSI Date 'YYYY-MM-DD' or ANSI Time 'HH:MM:SS' SQL formatting below for an implicit conversion with SQL ANSI DATE or SQL ANSI TIME operator.

SELECT x, y, z FROM table WHERE COLUMN_DATE = DATE '2017-05-05'
SELECT x, y, z FROM table WHERE COLUMN_TIME = TIME '23:59:59'
- according such "COL_" exists

A must-keep cheatsheet for quick reference PSOUG Datatypes

NB: For Toad users, check your NLS SELECT * FROM NLS_SESSION_PARAMETERS; ; your NLS_DATE_FORMAT is not default one DD-MON-RR.

Sign up to request clarification or add additional context in comments.

3 Comments

Must-read Oracle ressources Working with Dates in PL/SQL by Steven Feuerstein
Thanks, this helped indeed. Still strange for me as '5may2017' works in TOAD
That most likely is due to TOAD not being just passive. It has an internal date setting and may have noticed the date format and either issued an alter session or modified the query to specify the format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.