Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

Suppose i have 10 sql tables. but am using 3 tables regularly. remain 7 tables are not used.
How to find and backup unused sql tables based on start date and end date through c#.net.

thanks in advance.
Posted
Updated 30-Sep-11 21:09pm
v2
Comments
André Kraak 1-Oct-11 3:05am    
What are "unused SQL tables"?

Please share any relevant code with us, seeing the code might us help understand the problem you are facing.

If you wish to change your question use the Improve Question button.

SQL
SELECT DB_NAME( t.database_id) as DataBaseName, OBJECT_NAME ( t.object_id,t.database_id) as TableName,t.object_id
FROM	(SELECT	  
         ius.database_id
	,ius.object_id
	,SUM(ius.user_lookups+ius.user_scans+ius.user_seeks+ius.user_updates) as usage
FROM sys.dm_db_index_usage_stats ius
GROUP BY ius.database_id,ius.object_id
HAVING SUM(ius.user_lookups+ius.user_scans+ius.user_seeks+ius.user_updates)=0
	) t


This will give you all the tables that were not used since SQL Server instance started (please keep in mind that when the instance is restarted sys.dm_db_index_usage_stats will be reset to 0)
 
Share this answer
 
The following script will give you a list of last updated indexes on tables which you can use to get the last updated tables.
SQL
SELECT
t.name AS Table_Name
,i.name AS Index_Name
,i.type_desc AS Index_Type
,STATS_DATE(i.object_id,i.index_id) AS Date_Updated
FROM
sys.indexes i JOIN
sys.tables t ON t.object_id = i.object_id JOIN
sys.partitions sp ON i.object_id = sp.object_id
and i.index_id = sp.index_id 
WHERE
i.type > 0 and 
sp.rows > 0
ORDER BY
t.name ASC
,i.type_desc ASC
,i.name ASC 


The backup you will have to do yourself as the sql backup process does not support tables backup but full database backup.
 
Share this answer
 
Comments
Member 7831076 1-Oct-11 4:45am    
Hi Mehdi,

Thanks for your reply. But this query does not shown any result. what's the reason can you please help me.

thanks.
Mehdi Gholam 1-Oct-11 6:05am    
The query will only work if you have indexes defined on your tables.
Member 7831076 1-Oct-11 6:44am    
ok thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900