Library tutorials & articles

SQL Distributed Management Objects Part 2

The Job Steps

We have created a new job step object in the statements above. The jobstep object exposes the attributes of a single SQL Server agent executable job step. SQL Server Agent jobs contain one or more execution units called steps. Each job step contains a textual command, type of execution that specifies command interpretation, and logic that determines the behaviour of the job if the step succeeds or fails
       
       idStep = idStep + 1
       
       oJobStep.Name = JobID & idStep
       oJobStep.StepID = idStep
       
       'Set the job step executable subsystem.
       oJobStep.SubSystem = "TSQL"

       
The subsystem property specifies the SQL Server Agent execution subsystem used to  interpret job step task-defining text.
 
       If DatabaseName <> "" Then
          oJobStep.DatabaseName = DatabaseName
       Else
           oJobStep.DatabaseName = "yourdatabase"
       End If


If the user fails to pass the database name from the front end than the component will pick up the hardcoded database name provided that you have hardcoded the database name in your code.
       
       If idStep = "1" Then
           If CommandText <> "" Then
               oJobStep.Command = CommandText
           Else
               oJobStep.Command = "select * from table1"
               oJobStep.OnSuccessAction = SQLDMOJobStepAction_GotoNextStep
           End If
       Else
       
           oJobStep.StepID = 2
           If Commandtext2 <> "" Then
               oJobStep.Command = Commandtext2
           Else
               oJobStep.Command = "delete from table2"
               oJobStep.OnSuccessAction = SQLDMOJobStepAction_QuitWithSuccess
           End If
      End If

   
We have added two commands to the jobs, one will return all the records from the table and the second will delete all the records from the particular table, this has been done just to give you an example, you can do whatever you  want  with your database tables by passing the command text either from the front end or by hardcoding the command text in the code as seen above.

       oJob.JobSteps.Add oJobStep
   Next

       

Add the individual job step to the jobsteps collection.

   'Set the Target Server
   oJob.ApplyToTargetServer (CStr(Server))

   

The applytotargetserver method adds an execution target to the list of  targets maintained for the referenced SQL Server Agent job.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL Distributed Management Objects Part 2.

Leave a comment

Sign in or Join us (it's free).

S.S. Ahmed S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic we...

Related podcasts

  • Stack Overflow: Podcast #28

    This is the twenty-eighth episode of the StackOverflow podcast, where Joel and Jeff discuss Windows Azure, SQL Server 2008 full text search, Bayesian filtering, porn detection, and project management &mdash; among other things. Jeff met the inestimable Joey DeVilla aka Accordion Guy...

Want to stay in touch with what's going on? Follow us on twitter!