Read + Write + Report
Home | Start a blog | About Orble | FAQ | Blogs | Writers | My Orble | Login
Different ways to execute a SQL Agent job


Problem:
Every database person might have come across the situation of maintenance tasks such as backing up of databases, re-indexing tables and other such tasks. We often schedule jobs for such tasks, so that they execute as per the set schedule. But there is sometimes the need for these tasks to be executed “On Demand”. This tip shows various ways of executing such tasks on demand by any user regardless of whether the person is technical or not.

Solution
Let’s say I have a job called “BACKUPTEST” which backups the test databases. I want to be able to execute the job “On Demand”, so whenever anyone needs to do the backup this can be done. In this article I will show you how you can execute such Jobs easily through various ways.


In this tip we will look at these four methods:

SQL Server Management Studio
T-SQL commands
DMO (Distributed Management Objects)
OSQL

Also, this tip assumes that the jobs have already been setup.


----------------------------- ----------------------------- ----------------------

(1) - SQL Server Management Studio

The first way that most people are probably aware of is to use SQL Server Management Studio.

SQL Server Agent is the job scheduling tool for SQL Server.

To execute a job on demand using the GUI, open the SQL Server Agent tree, expand Jobs, select the job you want to run, right click on that job and click ‘Start Job’ and the job will execute.






----------------------------- ----------------------------- ----------------------

(2) -T-SQL commands

The second way is through a T-SQL statement using the 'sp_start_job' stored procedure which instructs SQL Server Agent to execute a job immediately. It is a stored procedure in the 'msdb' database.


The syntax for the sp_start_job stored procedure is:

sp_start_job
[@job_name] or [@job_id ]
[,@error_flag ]
[,@server_name]
[,@step_name ]
[,@output_flag ]



Arguments:

[@job_name] | [@job_id ] Is the name of the job to start. Either job_id or job_name must be specified, but both cannot be specified. job_name is sysname, with a default of NULL.

[@error_flag =] error_flag Reserved.

[@server_name =] 'server_name' Is the target server on which to start the job. server_name is nvarchar(30), with a default of NULL. server_name must be one of the target servers to which the job is currently targeted.

[@step_name =] 'step_name' Is the name of the step at which to begin execution of the job. Applies only to local jobs. step_name is sysname, with a default of NULL

[@output_flag =] output_flag Reserved.

When a job run it will have one of two return codes:

0 (success)
1 (failure)

To run the job ‘BACKUPTEST’ it can be executed by a single T-SQL statement: such as:

EXEC msdb.dbo.sp_start_job 'BACKUPTEST'




----------------------------- ----------------------------- ----------------------

(3) -DMO (Distributed Management Objects)





Another way of executing the job is through a VBS script using Distributed Management Objects (DMO).

Here is the basic script syntax.

On Error Goto 0: Main()
Sub Main()
Set objSQL = CreateObject("SQLDMO.SQLServe r")
' Leave as trusted connection
objSQL.LoginSecure = True
' Change to match the name of your SQL server
objSQL.Connect "Enter Server Name Here"
Set objJob = objSQL.JobServer
For each job in objJob.Jobs
if instr(1,job.Name,"Enter Job Name Here") > 0 then
msgbox job.Name
job.Start
msgbox "Job Started"
end if
Next
End Sub



Here is sample executing the "BACKUPTEST" job on server "SQLTEST1". This uses NT authentication to run this script.

On Error Goto 0: Main()
Sub Main()
Set objSQL = CreateObject("SQLDMO.SQLServe r")
' Leave as trusted connection
objSQL.LoginSecure = True
' Change to match the name of your SQL server
objSQL.Connect "SQLTEST1"
Set objJob = objSQL.JobServer
For each job in objJob.Jobs
if instr(1,job.Name,"BACKUPTEST") > 0 then
msgbox job.Name
job.Start
msgbox "Job Started"
end if
Next
End Sub



This code would then be saved in a file and named something like "RunJob.vbs". You can then double click on the file to execute it or run the code from a command line.


----------------------------- ----------------------------- ----------------------

(4) - Using osql utility

Lastly, we can start the job using osql commands.

The osql utility allows you to enter T-SQL statements, system procedures, and script files.

Here is the basic script syntax.

osql -S "Enter Server Name Here" -E -Q"exec msdb.dbo.sp_start_job 'Enter Job Name Here'"



Open a command prompt and execute the below osql command in it:, replacing your server name and job name.

osql -S "SQLTEST1" -E -Q"exec msdb.dbo.sp_start_job 'BACKUPTEST'"



The next step is to make a batch file which can be run over and over again.

Open notepad and type the commands as follow:

ECHO Executing job
ECHO.
pause
osql -s "MYPC\SQL" -E -Q"exec msdb.dbo.sp_start_job 'BACKUPTEST' "

ECHO job execution completed
pause
CLS
EXIT





Save the file as “job.bat”.


The batch is now ready for use. Just double click on it and it will do the maintenance work without having any knowledge of SQL Server.


----------------------------- ----------------------------- ----------------------


Permissions

You might have noticed in all the four solutions the msdb stored procedure ‘sp_start_job’ is used in one way or another.

By default, members of the sysadmin fixed server role can execute this stored procedure.

Other users must be granted one of the following SQL Server Agent fixed database roles in the msdb database:

SQLAgentUserRole
SQLAgentReaderRole
SQLAgentOperatorRole
Members of SQLAgentUserRole and SQLAgentReaderRole can only start jobs that they own.

Members of SQLAgentOperatorRole can start all local jobs including those that are owned by other users.

Members of sysadmin can start all local and multiserver jobs.



For details about the permissions of these roles, see SQL Server Agent Fixed Database Roles. and SQL Server Agent Fixed Database Roles



24
Vote
   


When was the last time your SQL Server database was restored



Problem
Often times we are asked the question "when was the last time my database was restored, and where was it restored from?" In this tip, we will look at some of the system tables that capture restore history information and how you can query these system tables to answer this question.

Solution
The restore history information is readily available inside the msdb, making the solution as easy as a few lines of T-SQL.


----------------------------- ----------------------------- ----------------------

Returning the details

Here is some T-SQL that will return information about the last time a database has been restored. There are two variables, @dbname and @days, that you can configure. The first (@dbname) would be the name of the database you are searching for and would need to be enclosed in single quotation marks. If you leave it NULL than all databases will be returned. The second variable (@days) would be a negative integer (i.e., -7) which represents how many days previously you want to search. So, -7 would translate to returning the previous week's worth of history. If you leave it NULL then the script will default to searching for only the previous thirty days.

DECLARE @dbname sysname, @days int
SET @dbname = NULL --substitute for whatever database name you want
SET @days = -30 --previous number of days, script will default to 30
SELECT
rsh.destination_database_name AS [Database],
rsh.user_name AS [Restored By],
CASE WHEN rsh.restore_type = 'D' THEN 'Database'
WHEN rsh.restore_type = 'F' THEN 'File'
WHEN rsh.restore_type = 'G' THEN 'Filegroup'
WHEN rsh.restore_type = 'I' THEN 'Differential'
WHEN rsh.restore_type = 'L' THEN 'Log'
WHEN rsh.restore_type = 'V' THEN 'Verifyonly'
WHEN rsh.restore_type = 'R' THEN 'Revert'
ELSE rsh.restore_type
END AS [Restore Type],
rsh.restore_date AS [Restore Started],
bmf.physical_device_name AS [Restored From],
rf.destination_phys_name AS [Restored To]
FROM msdb.dbo.restorehistory rsh
INNER JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id
INNER JOIN msdb.dbo.restorefile rf ON rsh.restore_history_id = rf.restore_history_id
INNER JOIN msdb.dbo.backupmediafamily bmf ON bmf.media_set_id = bs.media_set_id
WHERE rsh.restore_date >= DATEADD(dd, ISNULL(@days, -30), GETDATE()) --want to search for previous days
AND destination_database_name = ISNULL(@dbname, destination_database_name) --if no dbname, then return all
ORDER BY rsh.restore_history_id DESC
GO


The script will return the following result set:

Database RestoredBy RestoreType RestoreStarted RestoredFrom RestoredTo
AdventureWorks sa Database 2009-04-06 G:\Program... E:\Program...
AdventureWorks sa Database 2009-04-06 G:\Program... F:\Program...

Here is the definition of each of the result set columns.

ColumnName Description

Database: The name of the target database.

Restored By: The name of the user that performed the restore.

Restore Type: The type of restore performed. The possible types include the following:

D - Database
F - File
G - Filegroup
I - Differential
L - Log
V - Verifyonly
R - Revert

Restore Started: The time at which the restore command was started.

Restored From: The file(s) that the restore used in the RESTORE command.

Restored To: The database data files restored (or created) as a result of the RESTORE command.
27
Vote
   


Handling workloads on SQL Server 2008 with Resource Governor


Problem
Handling workloads has been quite difficult until SQL Server 2005. For example consider a scenario where one SQL instance is serving two applications i.e. an OLTP application and a reporting/data warehousing application. Since reporting applications are normally resource intensive, it may consume all the SQL Server available resources and may hamper the OLTP application which ideally should have more preference over the reporting application.

To handle this scenario, in earlier version of SQL Server there was one option to create multiple instances for these application (segregating by running one application on each instance) and setting CPU affinity mask for these instances appropriately. But the problems with this approach are, first it works for CPU only and second the dedicated CPUs cannot be shared by other SQL Server instances. For example, if there are two SQL Server instances and instance one has been assigned CPU 1 and 2 and instance two has been assigned CPU 3 and 4 on a four processor machine, even if instance one is idle and instance two is in need of additional resources, it can only use CPU 3 and 4. So what does SQL 2008 offer to solve this issue?

Solution
SQL Server 2005 resource allocation policies treat all workloads equally, and allocate shared resources as they are requested. It sometimes causes a disproportionate distribution of resources, which in turn results in uneven performance or unexpected slowdowns whereas the new Resource Governor of SQL Server 2008 allows organizations to define resource limits and priorities for different workloads, which enables concurrent workloads to provide consistent performance to the end users.

Resource Governor is a new technology in SQL Server 2008 that enables you to manage SQL Server workloads and resources by specifying limits on resource consumption by incoming requests. In an environment where multiple distinct workloads are present on the same server, Resource Governor enables us to differentiate these workloads and allocate shared resources as they are requested, based on the limits that you specify. These resources are CPU and memory. In other words, Resource Governor enables you to assign a relative importance to workloads. In other words, one workload can be allowed to proceed faster than another or is guaranteed to complete if there is resource contention. It allows a DBA or ITPros to define resource limits and priorities for different workloads.

Resource Governor Components

There are three new components of Resource Governor which are important to understand : resource pools, workload groups and classification (or classifier user-defined functions).

Pool: A resource pool, or pool, is a collection of system resources such as memory or CPU; it represents a portion of the physical resources of the server. Depending on its settings, a pool may have a fixed size (its minimum and maximum resource usage settings are equal to each other) or have a part which is shared between multiple pools (its minimum is less than its effective maximum). “Shared” in this case simply means that resources go to the pool that requests the resources first. In the default configuration all resources are shared, thus maintaining backward compatibility with SQL Server 2005 policies. Two resource pools (internal and default) are created when SQL Server 2008 is installed. Resource Governor also supports 18 user-defined resource pools. You specify MIN and MAX values for resources (CPU or Memory) which represents the minimum guaranteed resource availability of the pool and the maximum size of the pool, respectively. The sum of MIN values across all pools cannot exceed 100 percent of the server resources. MAX value can be set anywhere in the range between MIN and 100 percent inclusive. The internal pool represents the resources consumed by the SQL Server itself. This pool always contains only the internal group, and the pool is not alterable in any way. Resource consumption by the internal pool is not restricted. Any workloads in the pool are considered critical for server function, and Resource Governor allows the internal pool to pressure other pools even if it means the violation of limits set for the other pools. The default pool is the first predefined user pool. Prior to any configuration the default pool only contains the default group. The default pool cannot be created or dropped but it can be altered. The default pool can contain user-defined groups in addition to the default group.

Group: A workload group, or group, is a user-specified category of requests that are similar according to the classification rules that are applied to each session request. A group defines the policies for its members. A resource pool is assigned to a Workload Group, which is in turn is assigned to the Resource Governor. Two workload groups (internal and default) are created and mapped to their corresponding resource pools when SQL Server 2008 is installed, apart from that the Resource Governor also supports user-defined workload groups. The internal workload group is populated with requests that are for internal SQL Server use only. You cannot change the criteria used for routing these requests and you cannot classify requests into the internal workload group whereas requests are mapped to default workload group, if there is a classification failure, an attempt to map to a non-existent workload group and there is no criteria to classify. If the Resource Governor is disabled, all new connections are automatically classified into the default group and System-initiated requests are classified into the internal workload group.

Classification: Classification is a set of user-written rules that enable Resource Governor to classify session requests into the workload groups as described previously; for example classifying on the basis of user, application etc. It is implemented through a scalar Transact-SQL user-defined function (UDF) which is designated as a “classifier UDF” for the Resource Governor in the master database. Only one user-defined function can be designated as a classifier at a time.

Putting it all together

The incoming connection request for a session is classified by a classifier UDF and is routed to an appropriate workload group. This workload group in turn uses the resource pool associated with it and finally the resource pool provides and limits on the resources required by the session. Let's see this with an example, I will consider the same problem discussed in the problem section where we have one SQL Server instance serving an OLTP application and a reporting application, though it can be used in variety of different circumstances where you have to manage workloads:

First I will create two resource pools to be used by OLTP and Reporting application, then I will create two workload groups which will categorize the request coming from these applications.

Working with Resource Governor in SQL Server 2008 - Part 1

--Resource pool to be used by OLTP Application
CREATE RESOURCE POOL OLTPPool
WITH
(
MIN_CPU_PERCENT=50, MAX_CPU_PERCENT=100,
MIN_MEMORY_PERCENT=50, MAX_MEMORY_PERCENT=100
)
GO
--Resource pool to be used by Report Application
CREATE RESOURCE POOL ReportPool
WITH
(
MIN_CPU_PERCENT=50, MAX_CPU_PERCENT=100,
MIN_MEMORY_PERCENT=50, MAX_MEMORY_PERCENT=100
)
GO
--Workload Group to be used by OLTP Application
CREATE WORKLOAD GROUP OLTPGroup
USING OLTPPool ;
GO
--Workload Group to be used by Report Application
CREATE WORKLOAD GROUP ReportGroup
USING ReportPool ;
GO


Next I will create the classifier UDF to route incoming request to different workload groups and finally I will enable Resource Governor with ALTER RESOURCE GOVERNOR RECONFIGURE statement. Assumption here is, the OLTP application uses "OLTPUser" login whereas reporting application uses "ReportUser" login.

Working with Resource Governor in SQL Server 2008 - Part 2

USE master;
GO
CREATE FUNCTION dbo.ResourceClassifier()
RETURNS SYSNAME
WITH SCHEMABINDING
AS
BEGIN
--Declare the variable to hold the value returned in sysname.
DECLARE @WorkloadGroup AS SYSNAME
--If the user login is 'OLTPUser', map the connection to the
--OLTPGroup workload group.
IF (SUSER_NAME() = 'OLTPUser')
SET @WorkloadGroup = 'OLTPGroup'
--If the user login is 'ReportUser', map the connection to
--the ReportGroup workload group.
ELSE IF (SUSER_NAME() = 'ReportUser')
SET @WorkloadGroup = 'ReportGroup'
ELSE
SET @WorkloadGroup = 'default'
RETURN @WorkloadGroup
END
GO
--Register the classifier user-defined function and update the
--the in-memory configuration.
ALTER RESOURCE GOVERNOR
WITH (CLASSIFIER_FUNCTION=dbo.Reso urceClassifier);
GO
--Enabling Resource Governor(By default when you install
--SQL Server, Resource Governor is disabled)
--It loads the stored configuration metadata into memory
ALTER RESOURCE GOVERNOR RECONFIGURE
GO
--Disabling Resource Governor
ALTER RESOURCE GOVERNOR DISABLE
GO
--It resets statistics on all workload groups and resource pools.
ALTER RESOURCE GOVERNOR RESET STATISTICS
GO



Resource Governor can also be managed using SQL Server Management Studio (SSMS), you can CREATE, ALTER, DROP resource pools, workload groups, change classifier UDF and ENABLE/DISABLE Resource Governor .





Resource Governor’s Catalog Views and Dynamic Management Views

There are three new Catalog Views and three new Dynamic Management Views introduced for Resource Governor.

sys.resource_governor_configu ration - used to display the Resource Governor configuration as stored in metadata.

sys.resource_governor_resourc e_pools - used to display resource pool configuration as stored in metadata.

sys.resource_governor_workloa d_groups - used to display workload group configuration as stored in metadata.

sys.dm_resource_governor_conf iguration - used to get the current in-memory configuration state of Resource Governor

sys.dm_resource_governor_reso urce_pools - used to get the current resource pool state, the current configuration of resource pools, and resource pool statistics.

sys.dm_resource_governor_work load_groups - used to get the workload group statistics and the current in-memory configuration of the workload group.

In addition to new views that are specific to Resource Governor, existing system views have been modified to include information about Resource Governor as well.

Note

Only one resource pool can be assigned to a workload, though a single resource pool can serve multiple workload groups. If there are multiple workload groups in a given resource pool, you can set relative importance of each workload group to either LOW, MEDIUM or HIGH.

The resource governor limits can easily be reconfigured in real time with minimal impact on the workloads that are executing; for that purpose you use ALTER RESOURCE GOVERNOR statement with the RECONFIGURE parameter.

To monitor utilization of Resource governor you can monitor different performance counters under SQLServer:Resource Pool Stats and SQLServer:Workload Group Stats performance counter categories.

Limitations

There are also some limitations to the Resource Governor. They are as follows:

Resource management is limited to the SQL Server Database Engine. Resource Governor cannot be used for Analysis Services, Integration Services, and Reporting Services.

Only a single instance can be managed through this. An organization may have more than a single instance, but must manage each separately.

Limited to only two resources i.e. CPU bandwidth and memory management.

You are allowed to create only 18 resource pools apart from Default and Internal pool. Creating more resource pool than this throws an error, “The resource pool cannot be created. The maximum number of resource pools cannot exceed current limit of 20 including predefined resource pools.” Though in one sense it is good to have fewer resource pools and assign multiple workloads to it if required.

Resource Governor is available only on the Enterprise, Developer, and Evaluation editions of SQL Server.



41
Vote
   


Collecting performance counters and using SQL Server to analyze the data


[ Click here to read more ]
30
Vote
   


SQL Server UDF to convert integer date to datetime format

Problem


[ Click here to read more ]
29
Vote
   


Handling cross database joins that have different collations (SQL Server)

Problem


[ Click here to read more ]
32
Vote
   


Server level permissions for SQL Server 2005 and 2008

Problem


[ Click here to read more ]
28
Vote
   


Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)

Problem


[ Click here to read more ]
61
Vote
   


Dropping multiple objects with a single DROP statement


[ Click here to read more ]
40
Vote
   


Implementing Database Mirroring in SQL Server 2005 across domains


[ Click here to read more ]
67
Vote
   


More Posts
4 Posts
11 Posts
1 Posts
77 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:

Siddharth sood's Blogs

37 Vote(s)
0 Comment(s)
1 Post(s)
84 Vote(s)
0 Comment(s)
2 Post(s)
37 Vote(s)
0 Comment(s)
1 Post(s)
50 Vote(s)
0 Comment(s)
1 Post(s)
29 Vote(s)
0 Comment(s)
1 Post(s)
76 Vote(s)
0 Comment(s)
2 Post(s)
169 Vote(s)
0 Comment(s)
4 Post(s)
213 Vote(s)
2 Comment(s)
5 Post(s)
26 Vote(s)
0 Comment(s)
1 Post(s)
25 Vote(s)
0 Comment(s)
1 Post(s)
Moderated by Siddharth sood
Copyright © 2006 2007 2008 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]