0

I have a SQL file with variable in it. I want to get the SQL file where variable replaced by value I provide to sqlcmd. Can this be done?

I have:

USE AdventureWorks2012;

SELECT x.$(ColumnName)
FROM Person.Person x
WHERE x.BusinessEntityID < 5;

I want:

USE AdventureWorks2012;

SELECT x.Column1
FROM Person.Person x
WHERE x.BusinessEntityID < 5;

I hope it works something like:

sqlcmd -i template.sql -o compiled.sql -v ColumnName=Column1 --template-only
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
skfd
  • 2,528
  • 1
  • 19
  • 29
  • `sqlcmd` is not a pre-processor. It does not do textual replacement of variables, it substitutes the values at runtime. There is no need to "compile" anything, but if you want an output file with values replaced, you don't need `sqlcmd` but something like PowerShell and the `-replace` operator. Alternatively, you can use Management Studio's [template feature](https://technet.microsoft.com/library/ms179334) (this works differently from `sqlcmd` variables, and you do replace values in those). – Jeroen Mostert Mar 27 '18 at 17:20
  • `sqlcmd` has a pre-processor inside it, that does tamplating and extended commands stuff. I want to use this pre-processor without executing the query. Regexp replacing stuff by hand will be my last resort, but I'm considering it. – skfd Mar 27 '18 at 17:31
  • 1
    Oh sure, it pre-processes *in memory*, if you want to get technical -- but that output is never supplied to an output stream, so you can't get it. I'm not sure why you'd much care, because the replacement logic it applies is really very simple -- it does not do any complicated parsing or tokenization, just about anything that looks like `$(this)` gets replaced with exactly the string you supply. If this results in invalid SQL, `sqlcmd` doesn't even care. – Jeroen Mostert Mar 27 '18 at 17:33

0 Answers0