Archive

Archive for March, 2011

SQL – Check If A Table Exists

March 11, 2011 Leave a comment

Check if a table exists in a database:

IF OBJECT_ID(‘database.dbo.table’) > 0
BEGIN
   //Code to run if table exists. e.g. delete table.
   //drop table database.dbo.table
END

 

 

Categories: SQL Tags: , , , ,

SQL – How to get the status of an SQL job (sp_help_job)

March 8, 2011 3 comments

 

Here is a simple script using sp_help_job to display information about jobs that are used by SQL Server Agent:

USE msdb
EXEC dbo.sp_help_job @JOB_NAME = ‘JOB_NAME’, @job_aspect=‘JOB’

Look for ‘current_execution_status’ to get the status of the job.

1 = Executing.
2 = Waiting for thread.
3 = Between retries.
4 = Idle.
5 = Suspended.
7 = Performing completion actions.

 

Here is a more detailed script to store the results of sp_help_job into a table and query the results:

DECLARE @JOBNAME VARCHAR(100)
SET @JOBNAME = ‘JOB_NAME’

CREATE TABLE #JOBSTATUS(
       job_id uniqueidentifier 
      ,originating_server nvarchar(30)
      ,name sysname
      ,enabled tinyint
      ,description nvarchar(512)
     
,start_step_id int
     
,category sysname
     
,owner sysname
      ,notify_level_eventlog int
     
,notify_level_email int
     
,notify_level_netsend int
     
,notify_level_page int
     
,notify_email_operator sysname
     
,notify_netsend_operator sysname
     
,notify_page_operator sysname
     
,delete_level int
     
,date_created datetime
     
,date_modified datetime
     
,version_number int
     
,last_run_date int
     
,last_run_time int
     
,last_run_outcome int
     
,next_run_date int
     
,next_run_time int
     
,next_run_schedule_id int
     
,current_execution_status int
     
,current_execution_step sysname 
     
,current_retry_attempt int
     
,has_step int
     
,has_schedule int
     
,has_target int
     
,type int     
); 

INSERT INTO #JOBSTATUS
EXEC dbo.sp_help_job
 @JOB_NAME = @JOBNAME
,@job_aspect=‘JOB’

SELECT CASE current_execution_status
WHEN ’0′ THEN ‘Returns only those jobs that are not idle or suspended. ‘        
WHEN ’1′ THEN ‘Executing.’
        
WHEN ’2′ THEN ‘Waiting for thread.’
       
WHEN ’3′ THEN ‘Between retries.’
        
WHEN ’4′ THEN ‘Idle.’

WHEN ’5′ THEN ‘Suspended.’

WHEN ’6′ THEN

WHEN ’7′ THEN ‘Performing completion actions.’

ELSE ‘UNKNOWN’
END as ‘current_execution_status’
FROM #JOBSTATUS

DROP TABLE #JOBSTATUS

 

OUTPUT:


current_execution_status
——————————————————–
Idle.

SQL – How to get the day of the week

Using the DATEPART function to calculate the integer value of the day of the week.

select datepart(dw,getdate()) as DayOfWeekINT


Output:
DayOfWeekINT
————
2  

1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday

Using the DATENAME function to calculate the  day of the week.

select datename(dw,getdate()) as DayOfWeekCHAR


Output:
DayOfWeekCHAR
——————————
Monday
 
 

 

You can get the day of the week for a given date (MM/DD/YYYY) using:

select datepart(dw,’03/07/2011′) as DayOfWeekINT
select datename(dw,’03/07/2011′) as DayOfWeekCHAR

 

Follow

Get every new post delivered to your Inbox.