JDBC query with dynamic variables

Hi,
in a JDBC SQL I am running the following SQL and all is working fine:
SELECT dtdodt, dtrefx
FROM myfile
WHERE DATE(SUBSTR(CHAR(dtdodt), 1, 4) || '-' ||
SUBSTR(CHAR(dtdodt), 5, 2) || '-' ||
SUBSTR(CHAR(dtdodt), 7, 2)) >= DATE(2024-12-31)
FETCH FIRST 200 ROWS ONLY

However when I replace the date with the variable like you can see next I am getting an error:
SELECT dtdodt, dtrefx
FROM myfile
WHERE DATE(SUBSTR(CHAR(dtdodt), 1, 4) || '-' ||
SUBSTR(CHAR(dtdodt), 5, 2) || '-' ||
SUBSTR(CHAR(dtdodt), 7, 2)) >= DATE({{ Dateperiod6 }})
FETCH FIRST 200 ROWS ONLY

It seems like the variable is NULL. But the variable contains exactly the same: 2024-12-31.

I think the SQL cannot access the variable content.

I am new in Retool. Can someone guide me?

Thanks in advance

Dynamically query resources

I just wanted to update you on what I did more.
I have created a Transformer like you can see on the right of the printscreen:


The JDBC SQL is now only what you have on the left of the image: {{ AR_Aging_trf.value }} which contains exactly the SQL statement that I want to run:
image

This is still not working.

I may be wrong but I think that what is being sent to the JDBC driver is this and the ? is not replaced:
image

Like I said I am new in Retool and I would really appreciate if any one can give me any help.

Best

Hi @zepaulosilva,

Thanks for reaching out & welcome to Retool!

For the transformer approach, I would not expect this to work in Retool due to prepared statements. For example, prepared statements prevents you from putting the table name inside double curly brackets.

You can try disabling prepared statements on the resource settings, but we generally don't recommend this as it could have security risks and it could break other queries for this resource that are already working.

With prepared statements enabled, we're more likely to get your original query with just the dynamic variable working. What is {{Dateperiod6}}? If it is a Retool variable, you'll need to change it to {{Dateperiod6.value}}. I'd try that next and see if it works or if the error is any different. For troubleshooting, you could also try {{ '2024-12-31'}} to see if that works before we explore other syntax changes

Hi Tess,

Thank you for reaching out!

I did what you suggested and it still doesn't work:

I also did your other suggestion that did not work:

Let me remind you that this is a JDBC SQL.

Thanks in advance for all the support you can give me.

Thanks for the updated screenshots! What is the error message? Do you have any logs for this query on the resource side (outside of Retool)?

1 Like

You can see the error below:

Based on this "Use of parameter marker or NULL not valid" I think the issue is that what is being sent to the JDBC driver is the actual parameter marker ? instead of the variable content.

Could this be what's happening?

Thanks again for your help.

Hmm, since it happens when hardcoded, I think it's probably a syntax issue over sending null or ? instead of the date :thinking:

What about switching it to DATE({{'"2024-12-31"'}})? If that works, you may need to change the dynamic version to: DATE({{'"'+Dateperiod6.value+'"'}})

I tried what you suggested but it didn't work.

@Tess , what do you think I can do?
I don't think I'm doing anything wrong but I might.

Hi @zepaulosilva,

This is a tricky one :thinking: especially since I don't have the exact same resource to test.

Were you able to track down any external logs? Did you confirm if you have prepared statements enabled on the resource settings?

It may help to discuss this live if you can join office hours on Tuesday

I have sent you the error messages I get from the server.
As for the Resource, yes, I have prepared statements enabled.
image

I am travelling and I will not be able to join you for a live discussion on Tuesday. Maybe Thursday or Friday.

Do you know if anyone is using JDBC queries with parameter markers to be replaced?