I'm a bit new to SQL queries and I want to make my life easier by using one query to search a complex table, but change out a variable in the "WHERE x IN @variable1" sort of deal. The background on this is that I have a massive database of computer data that is inventoried daily and updated with details relevant to my job. Every week I need to perform a query for several different results from the same database pulling the same tables, but I need to use different criteria.
For example, my SQL query is HUGE (pulling over 30 columns, performing joins, etc. It works perfect). However at the end I have a line that says something like:
WHERE COMPUTER.Features IN ('Online, 'Active', 'Recent')
But then I have to perform the same query, but swap out the "IN ..." part like this:
WHERE COMPUTER.Features IN ('Decommission', 'Offline', 'Refresh', 'Ticketed', 'In-Transit')
I have MANY different use cases (which are commented out in the same file) and every day I have to copy/paste the new set of "IN ..." conditions.
What I'm thinking would work awesome is if I could just use variables to make my life easier. So I could just do something like:
WHERE COMPUTER.Features IN @Active
or
WHERE COMPUTER.Features IN @Problems
or
WHERE COMPUTER.Features IN @UserHelp
and so on. I have declared one variable as a test like this:
DECLARE @Active VARCHAR
SET @Active = '(''Online'', ''Active'', ''Recent'')'
That way instead of having to copy/paste all the values every time, I can just change @UserHelp to @Active and hit Go.
Is this possible? I've seen some other examples online and it seems overly complex for what I'm trying to do. Any help would be appreciated.