Question

When publishing a new database, I want to run a procedure only if the server has SQL Agent installed.

Therefor, I created a new SQLCMD variable called $(SqlAgent) and I want to add a condition (instead of checking if SQLAgent runs in msdb.dbo.sysjobs cause sometimes it is installed but doesn't run).

And I add the following

 IF $(SqlAgent)='exists'
 BEGIN
EXEC [INFRA]....

 END

This fails because of

incorrect syntax next to SqlAgent

How can I use sqlcmd variable in a condition?

Était-ce utile?

La solution

First of all, check if you are running your script window as sqlcmd. The try this instead

IF ('$(SqlAgent)' = 'exists')
Begin
    Exec ...
End
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top