troubleshooting Question

Changing a query from Hard Coded to using a table list

Avatar of Scotto13
Scotto13 asked on
Microsoft SQL Server
17 Comments2 Solutions256 ViewsLast Modified:
My company has a SQL database that contains many tables - a different table for each client.  There is one table that is used to identify the tables.  

I wrote this query and one for each client and now want to change it to adapt to the tables listed.  

select, postdateid, quarkfilename, isrendered, date_posted, category, categoryid,ropadid, convert(varchar(10),postDate,101) as postDate,categoryname, categorytable, newspaperids
from prodSQL1.Prod_Admin.dbo.VancouverIsland
join prodSQL1.Prod_Admin.dbo.postDates as p on = VancouverIsland.postDateID
join prodSQL1.Prod_Admin.dbo.categories as cat on cat.ID = p.CategoryID
where postdate between @StartDate and @EndDate
and IsRendered <> 2

The table - VancouverIsland is identified in a table called Categories in a field called 'CategoryTable'.  

So, do I something like this?

Declare @tablename string
Set @table name = dbo.Categories.CategoryTable
select, postdateid...

Please help.
Join our community to see this answer!
Unlock 2 Answers and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros