SQLCMD :r <path> where path is a variable

不羁岁月 提交于 2019-12-23 09:33:50

问题


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: .

回答1:


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.




回答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)" + "%"



回答3:


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



来源:https://stackoverflow.com/questions/5653103/sqlcmd-r-path-where-path-is-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!