Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

create table with dynamic table name.

$
0
0

I'm trying to create a table

like

select x.*,  convert(nvarchar(20), getdate(), 101) AS LoadDate
into table1_20140512
from (
       select c1,c2,c3 from table2_20140512
       WHERE(LoadDate = (select MAX(LoadDate) FROM table2_20140512  )

       union all
    select c1,c2,c3 from table3_20140512
      WHERE(LoadDate = (select MAX(LoadDate) FROM table3_20140512  )
) X

I want to make table name dynamic, like 'table1'+toady's date

I declared three variables, but they didn't work as I expected  

These are my variables

DECLARE @table1 nvarchar(500)

DECLARE @table2 nvarchar(500)

DECLARE @table3 nvarchar(500)

SET @table1='H1_' +(CONVERT(VARCHAR(8),GETDATE(),112))

SET @table2='H2_' +(CONVERT(VARCHAR(8),GETDATE(),112))

SET @table2='H3_' +(CONVERT(VARCHAR(8),GETDATE(),112))


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>