Monday, June 20, 2016

SQL Joining Strings for a path referencing external file

Leave a Comment

I'm trying to join some strings together to define a path - for example, given $(name) = "PATH", I want :r .\PathOne\PATH.sql. The query fails at the first :r due to Syntax Error. If I hardcode the paths, and leave $(name) in the conditionals, it works as expected. It's just the string construction for the path that's failing for some reason.

 IF '$(name)' LIKE 'TEST%'  BEGIN    :r .\PathOne\'$(name)'.sql  END  IF '$(name)' NOT LIKE 'TEST%'  BEGIN    :r .\PathTwo\'$(name)'.sql  END 

How do I go about joining strings for a path in SQL? Naming the files directly works.

2 Answers

Answers 1

Enclose the hardcoded Parts of the path inside a double quote and your variables without any space next to hardcoded part for Constructing a path, For example

:setvar filename "test" :setvar root "D:\temp" :r  $(root)"\test\"$(filename)".sql" 

will result in path like

D:\temp\test\test.sql

To resolve your problem try something like this

:r ".\PathOne\"$(name)".sql" 

Hope this helps

Answers 2

I know that it may not sound intuitive, but try putting the path in double-quotes, like below. This works on my machine. Without the double-quotes, I get the same failure as you.

:setvar name "test"  if '$(name)' like 'test%' begin     :r "c:\temp\test1\"$(name).sql end else if '$(name)' not like 'test%' begin     :r "c:\temp\test2\"$(name).sql end 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment