Solved

How can I create a SP to Shrink the log? to be able to call it from a SQL Job.

Posted on 2008-10-28
9
221 Views
Last Modified: 2013-11-30
Hello:

I'd like to create a SP to shrink an SQLlog and then call it from a job, in the step after backing up the log.

create PROCEDURE truncatelog
DBCC SHRINKFILE (CEGE_SQL_Log, 1)

This gives me a syntax error.

Regards,
Joseph
0
Comment
Question by:CEGE
9 Comments
 
LVL 1

Author Comment

by:CEGE
ID: 22826827
hello:

What I'd like to do is do a database backup once a day and do log backups 3 times per day, and after each log backup I'd like to truncate the log, I have the log file limit set to 5GB, not unlimited growth. This seems to cover all of the bases should something bad happen. Is this correct? and not freeze up the server should the limit be breached.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 22826900
You should be able to use dbcc directly in the job steps. However, if you want to use a procedure, you will have to use something like this:

create PROCEDURE truncatelog

begin

  DBCC SHRINKFILE (CEGE_SQL_Log, 1)

end

Open in new window

0
 
LVL 1

Author Comment

by:CEGE
ID: 22827344
1) in the job step, it gave me this error:

Cannot shrink log file 2 (Goldmine_Log) because all logical log files are in use.

2) and it gave me a syntax error with Begin trying to create the SP:

What am I missing here?

thanks,
Joseph
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22827482
More importantly you should not be shrinking your log file.  Constantly growing and shrinking your file is not only extremely inefficient, it will fragment the ldf file across the disk making use of it by SQL server slower.

Your best bet is to find the maximum size you will EVER need, add 10%, and set it to a fixed size.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22827668
Since you are truncating the log, it would appear you have no interest in point-in-time restore, so why not set your Recovery Model to Simple, that way you
A) Don't have to backup your Transaction Log and more importantly
B) Don't have to shrink it repeatedly, either, which is a big no, no.
0
 
LVL 1

Author Comment

by:CEGE
ID: 22829204
hello acperkins:

The deal is that I do want a point in time restore and I also do want to do backups of the logs every 3 hours. But what I am not so sure about is the maximum size of the log, recently I set it the 1GB and it got hit after 2 weeks. I somehow would like to do the log backups and then have the file be as small as possible, but I don't want to repeatedly shrink it either. Does it get done automatically so that the max is never reached¿ What is the Best Practice for dealing with this? What do you do?
thanks,
Joseph
0
 
LVL 22

Expert Comment

by:PedroCGD
ID: 22829666
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 22830496
From what I've read here, my guess is that real issue isn't "How do I shrink the log file"... the real issue is "How to I keep the log file from growing"
If you're up for some light reading, I'd suggest the following article as it explains the steps required to manage log file growth: http://home.hot.rr.com/graye/Articles/SQL_LogMaintenance.htm
0
 
LVL 1

Author Closing Comment

by:CEGE
ID: 31510960
Finally, this is just what I was looking for. Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

932 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