Solved

TSQL Syntax To Automate Database Task

Posted on 2014-01-03
2
284 Views
Last Modified: 2014-01-18
Hi,

We have several MSSQL2005 and 2008 servers with hundreds of small databases. Most databases contain a table called "EventLog". In some cases these tables grow to millions of records and cause bloat or cause databases to reach maximum file size constraint limit.

I'm looking for a TSQL scrip that can be executed on the MSSQL server using scheduled task that could loop through each hosted database and run a TRUNCATE command on the "EventLog" table.

Currently we use external process to perform this task, however I would like to simplify this so it can be included with the daily backup routine.
0
Comment
Question by:ihost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39755144
You can use the below

DECLARE @SQL NVARCHAR(1000)
DECLARE @DBName NVARCHAR(150)
DECLARE DatabaseNames_Cursor CURSOR FOR
SELECT name FROM sys.databases WHERE name not in ('tempdb','master','msdb')

OPEN DatabaseNames_Cursor
FETCH NEXT FROM DatabaseNames_Cursor
INTO @DBName

WHILE @@FETCH_STATUS = 0 
BEGIN
   
   SET @SQL = ' IF EXISTS (SELECT 1 FROM sys.tables WHERE name = ' + '''' + 'EventLog' + '''' + ' ) TRUNCATE TABLE EventLog '
   SP_EXECUTESQL @SQL
   FETCH NEXT FROM DatabaseNames_Cursor
   INTO @DBName

END

Open in new window

0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39766241
You can also do something like this:
exec sp_MSforeachdb 'IF DB_ID(''?'') > 4 and ''?'' <> ''distribution''
begin
	IF EXISTS (SELECT 1 FROM [?].sys.tables WHERE name = ''EventLog'' ) 
	delete	from	?..EventLog
	where	EventDate < DateAdd(day, -5, GetDate())
end'

Open in new window


You can change it to TRUNCATE TABLE, but if you want to keep history for a certain amount of time, this will delete records older than five days.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question