0

I just answered this question but never tried to run it. So I tried it.. https://stackoverflow.com/questions/53521410/sql-command-mode-sql-server-unable-to-print/53521635#53521635

and I thought if Print works then the command will run for sure but to my surprise it didn't

USE [DBName] 
 :setvar ScriptPath 'C:\Work\'
 :setvar SQLFile 'Test.sql'
 ----- commented PRINT $(ScriptPath) + $(SQLFile) ---- Works 
 GO
 :r $(ScriptPath) + $(SQLFile) --- Doesn't work 
-- throws:A fatal scripting error occurred. Incorrect syntax was encountered while parsing :r
 :r $(ScriptPath)+$(SQLFile) --- Doesn't work 
 -- throws:A fatal scripting error occurred. Unable to process :r command
 GO

I searched but couldn't find correct answer.

What is needed to be fixed here ?

asked Nov 28, 2018 at 15:35

1 Answer 1

1

I used double quotes in your setvar commands and removed the (+) signs from your attempted concatenation. This worked for me

:setvar ScriptPath "C:\Work\"
:setvar SQLFile "Test.sql"
print '$(ScriptPath)$(SQLFile)' ---- Works 
GO
:r $(ScriptPath)$(SQLFile) ---- Works 
GO
answered Nov 28, 2018 at 16:13
1
  • yeah.. thanks.. with double quotes :r worked. But Print doesn't . Print works only with single quotes and + . Well anyway this solved my issue. Commented Nov 29, 2018 at 6:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.