?
Solved

SQL - concatenated table name

Posted on 2011-10-07
4
Medium Priority
?
261 Views
Last Modified: 2012-08-13
In this statement, I have to manually change the date of the table name everytime when I want to run this code:

 
SELECT *
INTO TRP.Cap_Data_20111007
FROM TRP.Cap_Data

Instead of manually changing it, can I do something like this?

DECLARE @yyyymmdd nvarchar(8)
SET @yyyymmdd = convert(varchar(8),getdate(),112)
 
SELECT *
INTO TRP.Cap_Data_ & '@yyyymmdd'
FROM TRP.Cap_Data

I'm not sure if there's a way to do this, but I'm not able to figure out how to. Please help.
0
Comment
Question by:HNA071252
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36933783
yes, dynamic sql would be the way
DECLARE @yyyymmdd nvarchar(8)
SET @yyyymmdd = convert(varchar(8),getdate(),112) 

DECLARE @SQL nvarchar(1000)

SET @SQL = ' SELECT * 
INTO TRP.Cap_Data_' + @yyyymmdd + '
FROM TRP.Cap_Data '
exec(@sql)

Open in new window

0
 

Author Comment

by:HNA071252
ID: 36934168
That's awesome! One more thing, what do I do if I need to add the time to the end of the table name?

i.e. 20111007_1525
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36934196
same technique:
DECLARE @yyyymmdd nvarchar(8)
DECLARE @time nvarchar(4)
SET @yyyymmdd = convert(varchar(8),getdate(),112) 
SET @time = replace(convert(varchar(5), getdate(), 8), ':', '')

DECLARE @SQL nvarchar(1000)

SET @SQL = ' SELECT * 
INTO TRP.Cap_Data_' + @yyyymmdd + '_' + @time + '
FROM TRP.Cap_Data '
exec(@sql)

Open in new window

0
 

Author Closing Comment

by:HNA071252
ID: 36934220
Thank you very much!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question