it is normal that if you open a new tab in SSMS and switch to sqlcmd, the command
print $(ESCAPE_SQUOTE(SRVR))
does not work, since $(variable) is the syntax for using variables in sqlcmd, hence the error message.
Note that tokens are only to use within the SQL Agent scope.
https://learn.microsoft.com/en-us/sql/ssms/agent/use-tokens-in-job-steps?view=sql-server-2017
I tried to reproduce your error in 2 instances I got handy, SQL2014 and SQL2016 and I created the following job, which works just fine on both versions
USE [msdb]
GO
/****** Object: Job [token test] Script Date: 14/06/2018 21:14:35 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 14/06/2018 21:14:35 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'token test',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [step1] Script Date: 14/06/2018 21:14:35 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'step1',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'CmdExec',
@command=N'sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "SELECT @@servername"
',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
So it must be something else, since SQL2016 in this specific matter behaves just as previous version.
/******** Edit ****************/
I have created the above SQL Agent job in a machine which runs Win Server 2016 - SQL Server 2017 both Standard Edition and it just runs fine, as I pointed earlier maybe it's worth trying something simple like
SELECT @@SERVERNAME
to try narrow the problem down
Operating System (CmdExec)
, right?EXEC msdb.dbo.sp_set_sqlagent_properties @alert_replace_runtime_tokens = 1
and restarted sql Agent as well ?c:\Windows\System32\cmd.exe /c sqlcmd -E -S MyExplicitServerName -d master -Q "EXECUTE MyStoredProc etc..."
in this sort of format has any effect as well. Tag me back and I will explain if that helps you but whenc:\Windows\System32\cmd.exe /c
if prepended before thesqlcmd
command this will ensurecmd.exe
executes thesqlcmd
within its shell or whatever as PowerShell can interpret$(ESCAPE_SQUOTE(SRVR))
as a variable ifpowershell.exe
executes this code.