Adding columns in case statements

  • SELECT

    CASE

    WHEN EXISTS ( SELECT name FROM msdb.dbo.sysjobs WHERE name = 'JobA' )

    THEN 'Exists'

    ELSE 'Does not Exists'

    END as 'JobName'

    How do I add additional column values from enabled column from sysjobs

    If the job is present then check the status and print "enabled" or "disabled" under enabled column value

    If the job is not present then print "job not present" under enabled column value

    Output:

    JobName| Status

    JobA enabled

    JobName|Status

    JobA   disabled

    if JobA is not present:

    JobA Job not present

     

  • You can put it in a variable.  Just not sure why you would need the NotExists part.  What are you trying to accomplish?  I don't know how you would get JobA in the first place

     

     

    Declare @JobName varchar(100) = 'JobA'

    SELECT

    CASE

    WHEN EXISTS ( SELECT name FROM msdb.dbo.sysjobs WHERE name = @JobName )

    THEN @JobName + ' Exists'

    ELSE @JobName + ' Does not Exist'

    END as 'JobName'

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thanks.

    I need the job and its status if the job exists

    If the job does not exist it needs to print out the

    JobName under name column and Does not exists under the status column.

    If Job A exists and is enabled

    output :

    Job              Status

    JobA           enabled

    If JobA exists and is disabled

    output:

    Job             Status

    JobA          disabled

    if JobA does not exist

    output:

    Job           Status

    JobA        Does not exists

    I need 1 query to do this

    Thanks

     

    • This reply was modified 4 years, 1 month ago by  mtz676.
    • This reply was modified 4 years, 1 month ago by  mtz676.
    • This reply was modified 4 years, 1 month ago by  mtz676.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply