Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSQL Syntax To Automate Database Task

Posted on 2014-01-03
2
Medium Priority
?
299 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
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

972 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