Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLCMD :r <path> where path is a variable

Tags:

sqlcmd

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: .
like image 738
Serguei Avatar asked Apr 13 '11 17:04

Serguei


3 Answers

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.

like image 136
zifot Avatar answered Nov 12 '22 07:11

zifot


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)" + "%"
like image 44
Gary Thomann Avatar answered Nov 12 '22 09:11

Gary Thomann


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

like image 1
evhen14 Avatar answered Nov 12 '22 07:11

evhen14