query automatically table which name contains day's number

Hello,

I have automatically perform a select query on a table which contain  day depending on the database to insert the data into another table:

example :  the 13/10, a select is executed on the test_day_13 table and insert data into the table stats, the 14/10 a select is executed on the test_day_14 table and insert the data in the table stats

Thanks

Regards
bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You will have to use dynamic sql for this.  Since you didn't provide much in the way of schema you will have to fill in the brackets appropriately.

DECLARE @SQL NVARCHAR(4000);

SELECT @SQL = 'INSERT stats (<column list>) SELECT <column list> FROM test_day_' + CAST(DAY(GETDATE()) AS NVARCHAR);

sp_executsql (@SQL);
0
PortletPaulfreelancerCommented:
You can get the day portion of getdate() and form a tablename like this:

select
       N'test_day_' + convert(nvarchar(2),datepart(day,getdate()))
;

To place that into a query, do it like this because you need "dynamic sql", and then execute that string as your query.

nb, for debugging if you have issues display the @sql using select and don't try to exec(@sql) at the same time
declare @tblName as nvarchar(30) 
set @tblName = N'test_day_' + convert(nvarchar(2),datepart(day,getdate()))

declare @sql as nvarchar(max)

set @sql = N'select * from ' + @tblName

--select @sql -- uncomment to display the query (for debugging)

exec(@sql) 

Open in new window


See it working here: http://sqlfiddle.com/#!6/5d869/1
details
**MS SQL Server 2014 Schema Setup**:

    CREATE TABLE test_day_10    ([ID] int);
    INSERT INTO test_day_10     ([ID])
    VALUES
        (1),(2),(3),(4),(5)
    ;
    CREATE TABLE test_day_11    ([ID] int);
    INSERT INTO test_day_11     ([ID])
    VALUES
        (1),(2),(3),(4),(5)
    ;
    CREATE TABLE test_day_12    ([ID] int);
    INSERT INTO test_day_12     ([ID])
    VALUES
        (1),(2),(3),(4),(5)
    ;
    CREATE TABLE test_day_13    ([ID] int);
    INSERT INTO test_day_13     ([ID])
    VALUES
        (1),(2),(3),(4),(5)
    ;
    CREATE TABLE test_day_14    ([ID] int);
    INSERT INTO test_day_14     ([ID])
    VALUES
        (1),(2),(3),(4),(5)
    ;
    
**Query 1**:

    select
           N'test_day_' + convert(nvarchar(2),datepart(day,getdate()))
    

**[Results][2]**:
    |             |
    |-------------|
    | test_day_11 |
**Query 2**:

    
    
    declare @tblName as nvarchar(30) 
    set @tblName = N'test_day_' + convert(nvarchar(2),datepart(day,getdate()))
    
    declare @sql as nvarchar(max)
    
    set @sql = N'select * from ' + @tblName
    
    --select @sql -- uncomment to display the query (for debugging)
    
    exec(@sql) 
    
    

**[Results][3]**:
    | ID |
    |----|
    |  1 |
    |  2 |
    |  3 |
    |  4 |
    |  5 |

  [1]: http://sqlfiddle.com/#!6/5d869/1
  [2]: http://sqlfiddle.com/#!6/5d869/1/0
  [3]: http://sqlfiddle.com/#!6/5d869/1/1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
darn, sorry Brian
0
bibi92Author Commented:
Thanks a lot regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.