Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

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
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

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);
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
darn, sorry Brian
Avatar of bibi92

ASKER

Thanks a lot regards