We help IT Professionals succeed at work.

SQLCMD Syntax

245 Views
Last Modified: 2018-04-19
I'm trying to run a sqlcmd as follows

If I append say PRINT 'TEST' to the @command it errors out

What am I missing?

DECLARE  @command VARCHAR(256) 
DECLARE  @CommandText VARCHAR(512) 
DECLARE  @SQLCMD VARCHAR(64) 

SET @SQLCMD = 'SQLCMD -SLocalHost -E -Q ' 
SET @command = ':CONNECT SERVER1' 

SET @CommandText = @SQLCMD + '"' + @command + '"' 
PRINT @CommandText 

EXEC MASTER.dbo.Xp_cmdshell @CommandText

GO 

Open in new window

Comment
Watch Question

Senior Windows Automation Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Mauro CazabonnetSenior Windows Automation Developer
CERTIFIED EXPERT
Top Expert 2015

Author

Commented:
Never mind I can specify the remote server and database as a parameter in sqlcmd :)