Help Using Variable Results in Select Statement

I have a select menu in my program that grabs values from a table that only has two columns: Rating_ID and Rating_Name. The value it grabs is the Rating_Name.

I then have a table that will want to display information that will be partially contingent on the value from the selected value from that select dropdown.

The thing is, I need to use the Rating_ID in my query, not the Rating_Name. Therefore, I created a variable at the beginning of my SQL statement that grabs the value from the select dropdown and sets the value of the variable to whatever the Rating_ID is for that Rating_Name. I know that works because I've tested it in SQL and it always returns the right value.

The problem I'm having is getting it into the actual Select Statement for the output of my table. Assuming that the variable I've created is called @X and the query is pulling from a table called "Test" and I put in the following code it doesn't work:

SELECT Test.@X from Test

I've tried putting @X in brackets and that doesn't work either.

I have tested putting in the actual integer value (bypassing the variable, but also thus making the output static rather than dynamic based on the select dropdown) and it will work if I use the following code:

SELECT Test.[1] from Test

I've used variables a lot in my program and never had a problem, but I've never used it in a Select Statement and wasn't expecting this to be a problem. I'm wondering if it is my syntax or something else.

Any help would be appreciated!

Hello @akosinski28 -- welcome to the forums!

How/where are you defining @X? What does the table query return if you use select * from Test?

Hi @akosinski28! I'd like to echo @pyrrho's welcome. :wave:

I think I understand your question, but I'm going to actually suggest a solution that bypasses the need for SQL variables. One of the cool things about the select menu is that the label and value can be distinct. This means you can implement something like the below:

In this example, I'm populating the select menu with elements from the table but specifically mapping the rating_id and rating_name columns to the "Value" and "Label" fields. This way we obscure the id from the user but can still access it as the value of the "Select" component. I'm pretty sure this should solve your problem!