debugsqlMinor
SQL Server 2016 SQL Agent Token Not Working
Viewed 0 times
sqlagentworkingtokenservernot2016
Problem
I have this in scores of SQL Agent Job steps throughout my enterprise and it works as expected:
But on my new SQL Server 2016 instance, it simply produces a Named Pipes connection error (which is a complete red herring).
This, on the other hand, works just fine on my new server:
Why does the SRVR token not work?
If I fire up sqlcmd in a command prompt and tell it to print
This is a pretty basic, no-frills install with just the default instance.
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."But on my new SQL Server 2016 instance, it simply produces a Named Pipes connection error (which is a complete red herring).
This, on the other hand, works just fine on my new server:
sqlcmd -E -S MyExplicitServerName -d master -Q "EXECUTE MyStoredProc etc..."Why does the SRVR token not work?
If I fire up sqlcmd in a command prompt and tell it to print
$(ESCAPE_SQUOTE(SRVR)) it says:'SRVR' scripting vaiable not defined.This is a pretty basic, no-frills install with just the default instance.
Solution
Okay, I'm now thinking this is something with Windows Server 2016 as this is the first SQL Server 2016 install I've done on Windows Server 2016.
On my Windows Server 2012 R2 (Standard) machine with SQL Server 2016, this works in a SQL Agent CmdExec job step:
But on my new Windows Server 2016 (Standard) with SQL Server 2016, the above does not work. I have to do either:
Or this works as well:
The
-c batch_terminator
Specifies the batch terminator. By default, commands are terminated
and sent to SQL Server by typing the word "GO" on a line by itself.
When you reset the batch terminator, do not use Transact-SQL reserved
keywords or characters that have special meaning to the operating
system, even if they are preceded by a backslash.
I don't know why the batch terminator is necessary in this situation...
The GO Command and the Semicolon Terminator
The GO Command
“GO” is a batch terminator. Technically speaking, the GO command is
not even a part of the Transact-SQL language. It is really a command
used by the SQLCMD, OSQL and ISQL utilities that can also be used
within Query Analyzer and the Query Editor window.
NOTE: A batch should not be confused with a script. A batch is a set of T-SQL statements that are submitted for execution as a group. A
script is simply a file containing set of T-SQL statements. One script
can contain many batches.
On my Windows Server 2012 R2 (Standard) machine with SQL Server 2016, this works in a SQL Agent CmdExec job step:
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."But on my new Windows Server 2016 (Standard) with SQL Server 2016, the above does not work. I have to do either:
C:\Windows\System32\cmd.exe -c sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."Or this works as well:
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."
GOThe
cmd.exe -c argument (I was unfamiliar with) is a batch terminator; from Microsoft docs:-c batch_terminator
Specifies the batch terminator. By default, commands are terminated
and sent to SQL Server by typing the word "GO" on a line by itself.
When you reset the batch terminator, do not use Transact-SQL reserved
keywords or characters that have special meaning to the operating
system, even if they are preceded by a backslash.
I don't know why the batch terminator is necessary in this situation...
The GO Command and the Semicolon Terminator
The GO Command
“GO” is a batch terminator. Technically speaking, the GO command is
not even a part of the Transact-SQL language. It is really a command
used by the SQLCMD, OSQL and ISQL utilities that can also be used
within Query Analyzer and the Query Editor window.
NOTE: A batch should not be confused with a script. A batch is a set of T-SQL statements that are submitted for execution as a group. A
script is simply a file containing set of T-SQL statements. One script
can contain many batches.
Code Snippets
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."C:\Windows\System32\cmd.exe -c sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..."
GOContext
StackExchange Database Administrators Q#209689, answer score: 2
Revisions (0)
No revisions yet.