Solved

TSQL Syntax To Automate Database Task

Posted on 2014-01-03
2
283 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is this datetime? 1 26
too many installs coming along with SQL 2016? 1 28
Reformat SQL - so SSRS can read the columns 25 30
sql query display the latest row 10 51
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article I will describe the Copy Database Wizard 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 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