Guru's please help me with this.
I have a master table called ABC
, each month we create new table and suffix with month and year ABC_012010
, ABC_022010
.
I have to keep these tables for 7 years, and delete any table which are more than 7 years.
So the job would delete ABC_012010
in Feb 2017, ABC_022010
would be deleted in March 2017 and so on
The job runs every month to check and delete any table, which fits the criteria.
TRY THIS : You need dynamic query, loop and temporary tables
to perform this operation and your table format must be in the same format as you have mentioned in your example. You can perform INSERT, UPDATE, DELETE, DROP, TRUNCATE
in this way in multiple tables:
--Selecting table name in date format with `LEFT` `RIGHT` function
SELECT name,
CAST(CONCAT(LEFT(RIGHT(name, 6), 2), '-01-', RIGHT(RIGHT(name, 6), 4)) AS DATE) AS tableDate,
CAST(GETDATE() AS DATE) AS currentDate
INTO #tmp_date
FROM sys.objects
WHERE TYPE = 'u' AND NAME LIKE 'ABC[_]%'
--Storing table names that are older than 7 years from current date
SELECT id = IDENTITY(INT, 1, 1),
NAME
INTO #object_name FROM #tmp_date
WHERE (DATEDIFF(MM,tableDate, currentDate)-1) > = 84
DECLARE @i INT = 1, @j INT, @sql VARCHAR(500), @object_name VARCHAR(100)
SELECT @j = MAX(id) FROM #object_name
WHILE @i < = @j
BEGIN
SELECT @object_name = NAME FROM #object_name WHERE id = @i
SET @sql = 'DELETE FROM ' + @object_name --can use `DROP` to drop the table
EXEC (@sql)
SET @i = @i+1
END
try the following:
IF OBJECT_ID('TEMPDB.DBO.#TEMP_DEL', 'U') IS NOT NULL
DROP TABLE #TEMP_DEL
DECLARE @CYMO VARCHAR(8) = (SELECT '01' + RIGHT(REPLACE(CONVERT(CHAR(10), DATEADD(YY, -7, GETDATE()), 103), '/', ''), 6))
SELECT [NAME] TABLE_NAME INTO #TEMP_DEL FROM DBNAME.SYS.TABLES
WHERE [NAME] LIKE 'ABC%' AND '01'+RIGHT(NAME,6) < @CYMO
WHILE ((SELECT COUNT(1) FROM #TEMP_DEL) > 0)
BEGIN
DECLARE @TAB VARCHAR(100) = (SELECT TOP 1 TABLE_NAME FROM #TEMP_DEL)
DECLARE @CMD NVARCHAR(200) = 'DROP TABLE '+@TAB
EXEC SP_EXECUTESQL @CMD
DELETE #TEMP_DEL WHERE TABLE_NAME = @TAB
END
Replace DBNAME with your database name.
HTH!
Thanks.
You could use dynamic query, while and if conditionals
to construct a stored procedure or scheduled job (as you like) to solve this issue. Try this.
CREATE PROCEDURE dbo.delete_abc @Date date, @years_diff int
AS
--- Declare variables to use
DECLARE @sqlCommand varchar(30)
DECLARE @table varchar(20)
DECLARE @month int
DECLARE @year int
--- Temporary table with 'ABC_'s table names
SELECT TABLE_NAME
INTO #Temp
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'ABC_%'
AND TABLE_TYPE='BASE TABLE' -- If you want only tables
---- While there are tables to compare
WHILE EXISTS(SELECT * FROM #Temp)
BEGIN
--- Extract Month and Year of the first row in 'ABC_'s table
SET @month = (SELECT TOP 1 SUBSTRING(TABLE_NAME,5,2) FROM #Temp)
SET @year = (SELECT TOP 1 SUBSTRING(TABLE_NAME,7,4) FROM #Temp)
--- Compare if date difference of the table is more than @years_diff years from @Date
IF DATEDIFF(YEAR, CAST(CAST(@year AS VARCHAR) + '-' + CAST(@month AS VARCHAR) + '-01' AS DATE), DATEADD(MONTH, DATEDIFF(MONTH, 0, @Date), 0)) > @years_diff
BEGIN
--- Construct and execute delete query
SET @table = (SELECT TOP 1 TABLE_NAME FROM #Temp)
SET @sqlCommand = 'DROP TABLE ' + @table --- or 'DELETE FROM ' if you only want to delete all rows from table
EXEC(@sqlCommand)
END
--- Delete first row of 'ABC_'s tables in temporary
DELETE TOP (1) FROM #Temp;
END
--- Drop temporary table
DROP TABLE #Temp
GO;
Once it's created, you can use this stored procedure like this or with the parameters you like:
DECLARE @Now date
SET @Now = GETDATE()
EXEC dbo.delete_abc @years_diff = 7, @Date = @Now
进行删除查询是否足够容易?
Delete From [Your_Table] Where [Date_Column] = 'ABC_012010'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.