I'm trying to clean up a huge existing SQL script that's made up of several SQL queries that each get a number of records from one table and appends them to an "output table". It seems to me it would be so much easier to create one query to get all the records at once. The trouble is, each of these existing queries uses all kinds of criteria in their WHERE clauses.
I wonder if I could simply create a table containing all the WHERE criteria and then write some sort of SQL statement that uses all those criteria. Then if the criteria ever has to be changed I could just edit or add to that table, and not have to go through and rewrite the SQL.
For example, right now it's something like:
INSERT INTO SelectedCustomers (CustomerID)
SELECT CustomerID FROM Customers WHERE CustomerName LIKE '%Jones%' AND CustomerAge > '18'
Then further along there's:
INSERT INTO SelectedCustomers (CustomerID)
SELECT CustomerID FROM Customers WHERE CustomerState = 'CT' OR CustomerHair = 'Brown'
and further down is:
INSERT INTO SelectedCustomers (CustomerID)
SELECT CustomerID FROM Customers WHERE CustomerState = 'NY' AND CustomerHair <> 'Red'
etc (it goes on and on...)
Any ideas, links, etc?