I've setup a simple sqlcmd script. Contents below.
use $(dbname);
select * from $(tablename)
I'm then running the following Invoke SQLCMD
$SQLCMDVarArr = "dbname='MyDatabase'", "Tablename='MyTable'"
INVOKE-SQLCMD -ServerInstance MyServer\MyInstance -inputFile '\\MyShare\Database\Sprints\csrtemp2\csrtest.sql' -variable $SQLCMDVarArr
And I'm getting the error:
INVOKE-SQLCMD : Incorrect syntax near 'MyDatabase'.
I pulled the syntax straight from the invoke-sqlcmd get-help examples. Can anyone tell me what is wrong with this?
Thanks,
as per request this is running on windows Server 2008 R2
-
Could you please add which Operating System you are running the script on? It could be relevant seeing as Powershell is now available for Linux. Installing Windows PowerShellJohn K. N.– John K. N.2017年12月13日 14:09:46 +00:00Commented Dec 13, 2017 at 14:09
-
@hot2use - added the requested information (windows server 2008r2)Lumpy– Lumpy2017年12月13日 14:12:15 +00:00Commented Dec 13, 2017 at 14:12
-
1@hot2use While PowerShell is available on Linux, SQL Server PowerShell tools are not, they do not run nor install. Just FYI.user507– user5072017年12月13日 16:29:05 +00:00Commented Dec 13, 2017 at 16:29
1 Answer 1
So it turns out that the get-help invoke-sqlcmd -example shows the array variable as using 'dbname="MyDatabase"' as the syntax. However this gives the error above but if you swap the quote positions such that the variable uses "dbname='MyDatabase'" then the script works.
-
1Online version of the help seems to be showing correctly, here. The version that would be with the release of SQL Server will not get these updates. So just future reference good idea to check online version for some of those commands as they tend to show more accurate info.user507– user5072017年12月13日 16:31:49 +00:00Commented Dec 13, 2017 at 16:31