I'm writing a Python script to analyse some data, that is sourced from several different SQL databases. Queries to the databases are based on the results of previous queries, to different databases.
The problem that I'm having is that some queries may return NULL.
So, if I have a query like
SELECT *
FROM table_name
WHERE fieldA = ?
AND fieldB = ?
And I have two values that have been returned from other queries; field_a = 'A' and field_b = None, when I try to execute the query in Python:
with pyodbc.connect('Trusted_connection=yes', driver='{SQL Server}', server=server_name, database=database_name, autocommit=True) as connection:
ds = connection.execute(sql,[field_a, field_b])
nothing is returned. If I change the SQL query so that the None/Null value is hard-coded:
SELECT *
FROM table_name
WHERE field_a = ?
AND field_b IS NULL
I get the data that I expect.
Is there a better way to deal with this problem other than building the SQL query from substrings based on the field values?
AND field_b IS ??