Page 1 of 1

How to use a variable in a SQL query

Posted: Tue Feb 07, 2023 4:23 am
by jaimefuhr
I just discovered the ability to create a variable and specify that it can be used as a SQL parameter.

My old query looks like this: SELECT * FROM customers WHERE customerID=@custID

and I had created a parameter called @custID with type int.

I deleted the parameter and then created a variable called @custID and checked the box "Allow using as SQL parameter".

The report fails to run in the designer, crashing because of the "@" sign.

I changed the name of the variable to "custID" and my query to "SELECT * FROM customers WHERE customerID=custID".

It still fails to run, this time saying "Column custID is invalid".

How do you use a variable in the query?

Re: How to use a variable in a SQL query

Posted: Tue Feb 07, 2023 4:44 am
by jaimefuhr
Found the answer to my question. Need to surround the variable in brackets. Also it must be wrapped in quote if it is a string. e.g. SELECT * from customers WHERE firstName='{name}'

https://www.stimulsoft.com/en/blog/arti ... ql-queries

Re: How to use a variable in a SQL query

Posted: Tue Feb 07, 2023 7:30 am
by Lech Kulikowski
Hello,

Thank you for the information.