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.

You might also like...

Comments

About the author

S.S. Ahmed United States

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...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra