Closed mkmoisen closed 9 months ago
Can you show me what you did in SQL*Plus that worked?
@anthony-tuininga
Sorry, it only works in SQLDeveloper, not in SQLPlus.
I ran this in SQLPlus
var l_sysdate varchar2(128);
-- This does not work in sqlplus:
SELECT COUNT(1)
FROM foo
WHERE 1=1
AND NVL(:l_sysdate, SYSDATE) > c1;
-- This however with TO_DATE does work in sqlplus:
SELECT COUNT(1)
FROM foo
WHERE 1=1
AND NVL(TO_DATE(:l_sysdate, 'YYYY-MM-DD HH24:MI:SS'), SYSDATE) > c1;
In sqldevloper, this seems to work:
select count(1)
from foo
where 1=1
and nvl(:l_sysdate, sysdate) > c1
;
I'm not sure what sqldeveloper is doing to make this work though.
Thanks for the clarification. This is expected behavior. The value None
in Python which is translated to null
in Oracle does not have a "type". So the driver is forced to "guess" -- and the driver always uses VARCHAR2(1)
-- so your "workaround" is the correct way of telling the database what type you are actually using. I'm not sure what SQLDeveloper is doing. It may be analyzing the statement and adjusting accordingly.
@anthony-tuininga
Thanks. Do you happen to know if there is any more intuitive way to accomplish this, other than cur.setinputsizes(l_sysdate=oracledb.Date)?
I'm actually using SQLAlchemy on top of cx_Oracle and could not get it to work at all.
I tried variations on TO_DATE(:l_sysdate) but also couldn't get it working.
This works for me:
result = cur.execute(
'''
SELECT COUNT(1)
FROM issue_227
WHERE 1=1
and nvl(to_date(:l_sysdate), sysdate) >= issue_227.c1
''',
dict(
l_sysdate=None
)
).fetchone()
result = cur.execute(
'''
SELECT COUNT(1)
FROM issue_227
WHERE 1=1
and nvl(to_date(:l_sysdate), sysdate) >= issue_227.c1
''',
dict(
l_sysdate=datetime.datetime(2023, 10, 1)
)
).fetchone()
What versions are you using?
Give your database version.
Oracle 19c
Also run Python and show the output of:
And:
-->
Error
Using a null bind variable in this statement
AND NVL(:l_sysdate, SYSDATE) >= foo.c1
doesn't appear to work by default.It seems the only way to make it work is by calling
cur.setinputsizes(l_sysdate=oracledb.Date)
in advance.I tried doing this from both sqlplus and sqldeveloper, where it seems to work fine without having to specify any date data type.
Database setup:
Python:
It works fine when I use sqlplus or sqldeveloper. I don't need to specify any date datatype for it to work.
Use a gist for long screen output and logs: see https://gist.github.com/
-->
Yes
This tells us whether you are using the python-oracledb Thin or Thick mode.
Thick
See above