Creating a job

Following are the steps to create a job that prints Hello World.

StepsCreating a Hello World job and a list of its history

  1. Perform the server and user setup described in Chapter 2, “Configuring and Running Job Scheduler.” Be sure you have permission to log in to your target server using the user name and password that was supplied to the sp_addexternlogin stored procedure.

  2. In the following code example, use the sp_sjobcreate procedure to create a job:

    use sybmgmtdb
    go
    declare @jobcmd varchar(255), @jobid int
    select  @jobcmd='jcmd=print “Hello World.”,server=YOUR_SERVER'
    + ',starttime=' + convert(varchar(32),getdate())
    exec @jobid=sp_sjobcreate 'sjname=hello', @jobcmd
    go
    

    where

    YOUR_SERVER is the name of your target server

  3. Use sp_sjobhelp to look at the scheduled job that you created:

    > exec sp_sjobhelp 'sjname=hello'
    > go
    
  4. The following summary of the scheduled job is returned:

    sjob_id: 127  name: 'hello'
    owner          : jsadmin1
    created        : Jul 14 2005 4:42AM
    state          : enabled
    job name       : 114 - 'job_114'
    schedule name  : 115 - 'sched_115'
    server         : pgibson_js
    -- job --------: 
    description    : 
    owner          : jsadmin1
    created        : Jul 14 2005 4:42AM
    -- schedule ---:
    description    : 
    owner          : jsadmin1
    created        : Jul 14 2005 4:42AM
    starttime      : 04:42
    startdate      : 14 Jul 2005
    
  5. Use sp_sjobhistory to look at the short list history of executing a scheduled job:

    sp_sjobhistory 'sjname=hello', @option='list_short'
    go
    
  6. The following short list is returned:

    sjob_id  sjob_jobname  sjob_schedname  sjob_server    sjob_state
    -------  ------------  --------------  -----------    ----------
    127      job_114       sched_115       pgibson_js     C2
    
    sjob_start             sjob_user_run   sjob_user_req  sjob_size
    ----------             -------------   -------------  ---------
    Jul 14 2005 4:42AM    jsadmin1        jsadmin1       51
    
  7. Use sp_sjobhistory to look at the output from executing a scheduled job:

    sp_sjobhistory 'sjname=hello', 'list_output'
    go
    
  8. The following output is returned:

     jsout_run_id   jsout_seqno   jsout_size    jsout_text
    ------------   -----------   -----------   ---------- 
    144            0             51            Changed database context to 'master'.
    
    hello world