Skip to content
Advertisement

Updating SQL Variable

I’m attempting to create a variable that will hold a value, which I will then later use to select from a table using the variable value. This works perfectly the first time around, but any time I try and change the parameters to update the variable it returns blank. Am I able to use the same variable with different parameters? Or am I doing something wrong with my code?

Like I said, this method words great for 1 time and then afterward if I Print the variable then it returns blank.

Here is an example of my variable working as expected.

Now If I run the same exact query but change AND t1.IdReport = 3375 to AND t1.IdReport = 3595 the print @ReportTitle returns empty and the query I have written does not return any rows.

Here is an example of one that is not working:

My expected outcome is to use the variable to hold titles based on the IdReport I pass.

For example, the first time around the variable prints Retail which is what I am expecting, I then search for titles that includes %Retail%

Second time around I run the query using IdReport = 3595 which returns the title Appointments. Which if I print @ReportTitle it equals nothing when I expect it to equal Appointments

Advertisement

Answer

Was able to figure it out. It wasn’t a data issue so it was a bit tricky. Turns I may have had an extra SELECT that wasn’t needed.

Here is a code sample of working code for me:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement