How to use a variable in a SQL query

Stimulsoft Reports.WEB discussion
Post Reply
jaimefuhr
Posts: 8
Joined: Wed Sep 15, 2021 5:10 pm

How to use a variable in a SQL query

Post 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?
jaimefuhr
Posts: 8
Joined: Wed Sep 15, 2021 5:10 pm

Re: How to use a variable in a SQL query

Post 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
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: How to use a variable in a SQL query

Post by Lech Kulikowski »

Hello,

Thank you for the information.
Post Reply