emi_sastra
asked on
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.
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.
ASKER
Hi PortletPaul,
DELETE FROM 'TDJURNAL' + ' + @Year + ' + @IntMonth
Would you please how to code correctly for the above statement ?
Thank you.
DELETE FROM 'TDJURNAL' + ' + @Year + ' + @IntMonth
Would you please how to code correctly for the above statement ?
Thank you.
Please answer my question:
Do you want to "truncate" each table? read the documentation first, url provided above
Do you want to "truncate" each table? read the documentation first, url provided above
ASKER
I want to use variable for table name.
That's why I want to use looping.
Thank you.
That's why I want to use looping.
Thank you.
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.
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.
ASKER
Just want to delete data as I code.
Thank you,
Thank you,
ASKER
From table TDJURNAL201701, XXXX201702 and etc.
Using looping or any other better way.
Thank you.
Using looping or any other better way.
Thank you.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
- 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.
Yes, it is the right way.
Great. It works.
Thank you very much fo your help.
ASKER
Great.
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