Solved

TSQL Syntax To Automate Database Task

Posted on 2014-01-03
2
275 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 500 total points
Comment Utility
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.
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now