Delete table with year and month

Hi All,

I try to delete table data. Please see below :

WHILE ( @IntMonth <= 12)
BEGIN
    DELETE FROM 'TDJURNAL' + ' + @Year + ' + @IntMonth

      SET @IntMonth = @IntMonth + 1
END

For example table name TDJURNAL201701 to 201712.

How could I do it ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

PortletPaulEE Topic AdvisorCommented:
NO!

you won't get "leading zeros" to start with e.g. 1 <> "01"

and you cannot inject a table name into SQL through a variable like that, you need "dynamic sql" to do that.

Do you want to "truncate" each table?
https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql
0
emi_sastraAuthor Commented:
Hi PortletPaul,

    DELETE FROM 'TDJURNAL' + ' + @Year + ' + @IntMonth

Would you please how to code correctly for the above statement ?

Thank you.
0
PortletPaulEE Topic AdvisorCommented:
Please answer my question:  

Do you want to "truncate" each table? read the documentation first, url provided above
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

emi_sastraAuthor Commented:
I want to use variable for table name.

That's why I want to use looping.

Thank you.
0
PortletPaulEE Topic AdvisorCommented:
sigh....

Please answer my question:  

Do you want to "truncate" each table? read the documentation first, url provided above

only then can I provide more... I need to know if you want to truncate those tables, that is why I asked the question.
0
emi_sastraAuthor Commented:
Just want to delete data as I code.

Thank you,
0
emi_sastraAuthor Commented:
From table TDJURNAL201701, XXXX201702 and etc.
Using looping or any other better way.

Thank you.
0
PortletPaulEE Topic AdvisorCommented:
I really don't know how to help - fully - if I don't understand the nature of those deletion.

So I shall have to leave that to you to sort out. I assume below that you will truncate the tables.
declare @Year as int;
declare @intMonth as int;
declare @maxMonth as int;

set @Year = 2017
set @intMonth = 1
set @maxMonth = 5

declare @query as varchar(max);

WHILE ( @IntMonth <= @maxMonth)
BEGIN
   
   set @query = 'TRUNCATE TABLE TDJURNAL' + cast(@Year as varchar(4)) + right( '00' + cast(@IntMonth as varchar(2)),2)
   
   select @query
   execute(@query)
   
   SET @IntMonth = @IntMonth + 1
END

Open in new window


tested at: http://rextester.com/EPTM90228
2

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
emi_sastraAuthor Commented:
- I really don't know how to help - fully - if I don't understand the nature of those deletion.
Yes, it is the right way.

Great. It works.

Thank you very much fo your help.
0
emi_sastraAuthor Commented:
Great.
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.