8

Does the SQLCMD command :r support non-constant literal paths?

For example:

setvar $(path1) '.\script.sql'
:r $(path1) -- SQL01260: A fatal parser error occurred: .
:r '$(path1)' -- SQL01260: A fatal parser error occurred: .
:r "$(path1)" -- SQL01260: A fatal parser error occurred: .

3 Answers 3

7

Does the SQLCMD command :r support non-constant literal paths?

It does. You are defining your variable in a wrong way. Try:

:setvar path1 "script.sql"
:r $(path1)

See also this article on MSDN.

Sign up to request clarification or add additional context in comments.

2 Comments

is there a possibility like :r $(path)\module_v$(milesone)\01_AlterScript.sql
I've tried with just passing the value, I've tried upgrading to the latest mssql 20, and I've tried everything. Still no luck.
2

Just posting this here as an example for others to save the time I just lost.

test_setvar.sql file contains the following

-- :r test_setvar.sql
:reset

:setvar Name "filename"
print '$(Name)'
-- :setvar OutName '$(Name)'  -- NO wont work
-- :out $(OutName).txt
:r $(Name).sql  -- filename.sql exists and prints "Hello World"
:out $(Name).txt  -- filename.txt is created  

go
:out stdout

Stated clearly here.

Note: Double quotes needed for multi-part strings and fields with :out For example

:out "string bit"$(field1)"string bit2"$(field2)

Single quotes wrapping wont work with :out for this.

But... Double or single quote wrapping is needed within a query.

where col.name LIKE "%" + "$(SEARCH_STRING)" + "%"

Comments

1

The problem is not with the file VS lists in Error list but rather with the file you reference.

The syntax error should be searched for the file you reference. For your example .\script.sql

Comments

Your Answer

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.