Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dynamically change based on date
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

134 Posts

Posted - 02/26/2014 :  12:39:58  Show Profile  Reply with Quote

I have to change the name of the history table dynamically to what is passed in the start date.
If @start_date= '1/1/2014' then it should be history_jan14
If @start_date ='02/01/2014' then it should be history_feb14 and so on.
Is there a way you can do it ?
I am using SQL Server 2008.

Code :

DECLARE @start_date datetime
DECLARE @end_date datetime

SET @start_date = '01/01/2014'
SET @end_date = '02/01/2014'

select switch_hose,COUNT(*) as 'switch_count'
from history_jan14 hj (nolock),
inner join gap_orders ga (nolock)
on hj.switch_id = ga.switch_id

Thanks ,

Starting Member

11 Posts

Posted - 02/26/2014 :  12:53:15  Show Profile  Reply with Quote
I have to say...I'm not sure your database design is quite right. One table should hold all items. Having said that, you could use dynamic sql to query your different tables based on an input parameter.
Go to Top of Page

Flowing Fount of Yak Knowledge

4614 Posts

Posted - 02/26/2014 :  13:57:54  Show Profile  Reply with Quote
Agreed with Kennejd, that is a bad design.

One solution might be to create a view that unions all those history tables together, assuming they have a date column you can use to apply your range correctly.

Otherwise, as Kennejd mentioned, you'll have to use Dynamic SQL.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000