-
Need help with variables
In the database, different regions are identified by a region number. I have a long, complicated query and what I'd like to do is use a variable for the region number. That way if I want to run the query for a different region, I can change the region number in just one place instead of 5 places scattered throughout the query.
I've tried a few things based on examples I found online but I just can't get it to work for some reason. Help!
-
Create stored procedure for that, you can pass variable to sp.
-
I'm a beginner, so I'm sorry if this is a dumb question... but why do I need a stored procedure? isn't there a way to just declare a variable and set its value at the beginning of my query?
-
Yes you can, but you have to declare variable for each run. Besides, rdbms caches sp execution plan so better performance.
-
This query will be run ad hoc so I'm ok with setting the region number variable with each run.