Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Starting a Trace in Profiler

Author  Topic 

CameronY
Starting Member

4 Posts

Posted - 2007-01-03 : 07:19:41
Hello all,

Have a need to automate the execution of a SQL trace after the server is rebooted. A template has already been created and I've been asked to restart the trace/template first thing at the start of each week.

Sorry, my SQL utility knowledge is currently very limited and I'm learning as I'm exposed to more.

The goal is to automate the restart/execution of the trace template 30 minutes after the server is restarted.

Any suggestions appreciated.

Cheers,
Cameron

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-03 : 07:23:43
Schedule a task in the Windows Task scheduler for running profiler.exe along with the /T option.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2007-01-03 : 09:26:54
Why do you want to wait 30 minutes?

You can have a stored procedure automatically run when SQL Server starts using sp_procoption. Just make sure the trace file it creates has a unique name each time it runs.

===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page

CameronY
Starting Member

4 Posts

Posted - 2007-01-03 : 19:58:58
Many thanks for the feedback harsh & graz.

To define the goal a bit more:
When I've started the trace manually, I've done the following...
  • Start a VNC/console session
  • Start SQL Profiler
  • File > New > Trace
  • SQL Server = {local}
  • Connect Using: Windows Authentication
  • Click OK
Trace Properties:
  • Specify: Trace Name
  • Specify: Template Name
  • Checkbox: Save to file {identify destination & filename}
  • Set Max File Size: 10MB
  • Checkbox: Enable File Rollover
  • Commence Trace and lock console session.
Is it possible to do the above as you've suggested graz ??
The 30 minutes delay was just a figure I pulled out of mid-air, nothing specific about it.

Below is the current contents of the sp_procoption stored procedure found in the master db. I've little idea as to what to include/exclude/amend/etc to achieve the goal.
create procedure sp_procoption
@ProcName nvarchar(776)
,@OptionName varchar(35)
,@OptionValue varchar(12)
as
-- DECLARE VARIABLES
DECLARE @tabid int
,@uid int
,@intOptionValue int
,@dbname sysname

-- DISALLOW USER TRANSACTION --
Set nocount on
set implicit_transactions off
IF @@trancount > 0
begin
raiserror(15002,-1,-1,'sp_procoption')
RETURN @@ERROR
end

-- VALIDATE OPTION NAME AND VALUE
SELECT @intOptionValue =
CASE WHEN (lower(@OptionValue) in ('1' ,'on' ,'yes' ,'true')) THEN 1
WHEN (lower(@OptionValue) in ('0' ,'off' ,'no' ,'false')) THEN 0
ELSE NULL END
IF @intOptionValue IS NULL OR @OptionName IS NULL OR lower(@OptionName) <> 'startup'
BEGIN
raiserror(15600,-1,-1, 'sp_procoption')
RETURN @@ERROR
END

-- MUST BE sysadmin (Startup-procs run as sysadmin) --
IF is_srvrolemember('sysadmin') = 0
BEGIN
raiserror(15247,-1,-1)
RETURN @@ERROR
END

-- RESOLVE GIVEN OBJECT NAME --
SELECT @tabid = id, @uid = uid FROM sysobjects
WHERE id = OBJECT_ID(@ProcName, 'local') AND xtype IN ('X','P')

-- VALID OBJECT IN DATABASE? --
IF @tabid IS NULL
BEGIN
SELECT @dbname = db_name()
raiserror(15009,-1,-1 ,@ProcName, @dbname)
RETURN @@ERROR
END

-- STARTUP PROC MUST BE OWNED BY DBO IN MASTER --
IF (db_id() <> 1 OR @uid <> 1)
BEGIN
raiserror(15398,-1,-1)
RETURN @@ERROR
END

-- PROC CANNOT HAVE PARAMETERS --
IF EXISTS ( SELECT * FROM syscolumns WHERE id = @tabid )
BEGIN
raiserror(15399,-1, -1)
RETURN @@ERROR
END

-- Do the work
BEGIN TRAN
DBCC LockObjectSchema(@ProcName)
UPDATE sysobjects SET status = (status & ~2) | (2 * @intOptionValue) WHERE id = @tabid

-- Set Config option for startup procs
UPDATE master.dbo.sysconfigures SET value =
CASE WHEN EXISTS (SELECT * FROM sysobjects WHERE xtype IN ('X','P')
AND ObjectProperty(id, 'ExecIsStartup') = 1)
THEN 1 ELSE 0 END
WHERE config = 1547

-- If no error, commit and reconfigure
IF (@@error <> 0)
BEGIN
ROLLBACK TRAN
RETURN 1
END
COMMIT TRAN
RECONFIGURE WITH OVERRIDE

-- RETURN SUCCESS
RETURN 0 -- sp_procoption

GO
Hope this is all helpful.

Cheers,
Cameron
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2007-01-04 : 09:39:35
Cameron,

You shouldn't change sp_procoption at all.

First use Profiler to script out your trace. That will create a series of T-SQL statements that will start the script. Test this out. That's under File -> Save As Script or File -> Export depending on which version of SQL Server you're using (or at least the menu options are close to that).

Second, turn that script into a stored procedure. Test this out. Make sure this stored procedure is in the master database.

Third, lookup sp_procoption in Books Online. The syntax is something like this:

sp_procoption @ProcName = 'YourProcedure', @OptionName = 'startup', @OptionValue = 'true'

Executing that statement will set your procedure to run at startup. Please be aware that you're going to have issues with the file name unless you write some code to create a unique file name each time. There's a script on this page that has some code you can look at: http://www.cleardata.biz/cleartrace/beta.aspx






===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page
   

- Advertisement -