Why is it that you can pass a built string to xp_cmdshell but you can't build your string at the same time. What I mean is this:
This is legal:
DECLARE @cmd VARCHAR(255)
DECLARE @ServerName SYSNAME
SET @ServerName = 'Server1'
SET @cmd = 'copy C:\temp\test.txt \\ + @ServerName + '\SomeShare\'
EXEC master.dbo.xp_cmdshell @cmd
This is not legal:
DECLARE @ServerName SYSNAME
SET @ServerName = 'Server1'
EXEC master.dbo.xp_cmdshell 'copy C:\temp\test.txt \\ + @ServerName + '\SomeShare\'
Output:
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near '+'.
Any comments?