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

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Variable in Table Name?

Author  Topic 

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2010-11-18 : 14:02:43
Trying to use a variable in a table name since the tables are created 4 times a day.

I'd like to write a query for a DTS package that will account for the changing table names.

Example table names using yesterday and todays date:

T_S1117
T_D1117
T_T1117
T_N1117
T_S1118
T_D1118
T_T1118
T_N1118

Example query:
Select * from T_S1117

Looking for something like....

Declare @shift char
If datepart(hh,getdate()) between '00' and '08'
Begin
Set @shift = 'S'
End
Set @sDay = datepart(dd,getdate())
Set @sMonth = datepart(mm,getdate())

Select * from T_+ @shift + @month + @day

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-18 : 14:07:13
To do that search for "dynamic sql" and find this: http://www.sommarskog.se/dynamic_sql.html


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2010-11-18 : 14:58:30
First off..thanks...but can you help with the syntax? I think I am having an issue with the concatenating the table name with the variable. Probably the wrong single quotes or something.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-18 : 15:09:17
Please forgive me

EXEC('Select * from T_' + @shift + @month + @day)


Ok, now that you have your rope...please stop

This is SUCH a bad database design..

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2010-11-18 : 15:17:16
Why are the "gurus" always so E-cocky? You don't have all the information...I'm working with what is given to me and controlled on a corporate level...out of my hands. Maybe offer a suggestion rather than a comment such as the one you left?

BTW...thanks for the information....appreciated....almost.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-18 : 15:37:18
Did it work for you or not?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-18 : 17:36:34
quote:
Originally posted by ddobbertin

Why are the "gurus" always so E-cocky? You don't have all the information...I'm working with what is given to me and controlled on a corporate level...out of my hands. Maybe offer a suggestion rather than a comment such as the one you left?

BTW...thanks for the information....appreciated....almost.

Does the management know how bad this is and why? If not, you can do what I have done in the past and refused to do what they ask because they obviously don't understand the issues. Sometimes the CEO says: too bad, play hand you are delt. Other times, management gets bug-eyed and wonder who/how such an abomination was ever implemented. YMMV.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-18 : 18:38:51
Also, if was controled (and built) at the corporate level..ask them why they don'yt do it....can you tell us the corporation?

I know ONE name I'm sure I hope it's not....but wouldn't be surprised



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2010-11-22 : 17:14:09
No it did not....sorry was out of the office a couple days. So what is it that makes the design so bad? I guess I'm a little confused as to how you can tell the entire structure of the database from a couple of table names?

And no....I cannot tell you the corporation...lol.
Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2010-11-22 : 17:16:50
Also I might have been a little misleading with my original post...which then I could understand where you were coming from with the bad database design. I'm looking to use the variable in a query for the table name. I want to pull the queries 4 times/day and the tables are created for each shift each day. So I want the query dynamic to pull from each of the new tables. I've just been running into an issue with syntax.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-22 : 17:40:30
That's correct..the tables are created every day?

Why?

You should add columns to 1 table that identifies What the Table names represents...

The maintenance on the tables alone is a nightmare



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-22 : 17:47:49
Did you try what I gave you?


EXEC('Select * from T_' + @shift + @month + @day)


Now....look at this

USE [yourDB]
GO

SELECT SUBSTRING(TABLE_NAME,1,2) AS TABLE_PREFIX
, SUBSTRING(TABLE_NAME,3,1) AS [@shift]
, SUBSTRING(TABLE_NAME,4,2) AS [@sDay]
, SUBSTRING(TABLE_NAME,6,2) AS [@sMonth]
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME LIKE 'T_%'

Now...of you union all of those together you would have the 1 Table it should be



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-22 : 17:48:01
maybe

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -