In MS SQL Server Management Studio in SQLCMD Mode I use this script to run another script:
:setvar path "C:\workspace\scripts\scripts\"
:r $(path)'VERSIONS.sql'
But it gets this error:
A fatal scripting error occurred. The file specified for :r command
was not found.
VERSIONS.SQL is definitely in the specified folder. Why is this script not working when I run it?
EDIT: Something to note. The database I'm running this script against is not on my local machine. (I hope that's not the reason this isn't working)
I just solved my own problem. I changed the single quotes around VERSIONS.sql to double quotes and it worked.
Why are they not interchangeable?
Related
I am trying to get a SQL Server Agent job to execute in command line the following code. It sets the variable to the server name of the SQL Server instance so that it can reach out to the UNC path and run the executable. This singular line works find when I run it on the server in command line through RDP, but fails when run in SQL Server as type Operating system (CmdExec). The line is:
FOR /F "usebackq" %i IN (`hostname`) DO SET _exeCall=\\%i\test\test1.exe && %_exeCall%
The results from the log are:
C:\Windows\system32>SET _exeCall=\\testbox\test\test1.exe && _exeCall
'%_exeCall%' is not recognized as an internal or external command,
operable program or batch file.
The machine is correctly titled testbox, the path and executable file are correct, but it does not appear to understand that it should be executing the variable _exeCall. This works properly when executed on the server in commandline, but not from SQL Server's commandline tool.
I recently have started to use Beelines CLI to interface with a hive server.
The problem is that create file command is failing for me.
I have tried the following:
add FILE[S] 'example.txt';
Which returns this error:
Error: Error while processing statement: null (state=,code=1)
You should remove the quotes from the path. i.e.
beeline> add file example.txt;
Also be sure that you are only adding files to the server hive is running on.
I have a requirement to start ssms (SQL Management Studio) from the command line that connects to a (localdb)\myinstance. The instance exists
sqllocaldb info myinstance
but using the command line and the -S parameter with (localdb)\myinstance throws an error.
PS C:\workingArea> ssms -S(localdb)\myinstance
localdb : The term 'localdb' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:9
+ ssms -S(localdb)\myinstance
I can connect using the named pipe but would ideally like to use the known name.
You can try the following: ssms -S LOCALHOST\myinstanceto connect to a LocalDb instance
I'm using Powershell 4 to install SQL 2014. everything goes ok except at the very end where I have a function that will run a script from a .sql file using invoke-sqlcmd. I get the following error:
"The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file..."
If I try and import the sqlps module I get:
The specified module 'sqlps' was not loaded because no valid module file was found in any module directory.
But here's the kicker. If I open a separate PowerShell terminal, IT WORKS THERE. :/ and continues to fail in the initial terminal.
I'm trying to understand why this is so any assistance would be greatly appreciated. I'd like to avoid writing in a reboot once script.
Thanks,
Dan
Existing Powershell session isn't aware about Sql's modules that were just installed. Take a look at environment variable $env:PSModulePath. Compare new shell's variable to existing and you should see a missing path like ...\Microsoft SQL Server\110\Tools\PowerShell\Modules\.
For a work-around, modify the path to include module dir. Like so,
$env:PSModulePath += ";c:\some\path\to\sql"
I am trying to copy a file in my Pre Deployment script for a Visual Studio SQL Database project.
:!!copy $(DataFileDirectory)\data.csv $(DataDestinationDirectory)
I found that this generated the error:
SQL72007: The syntax check failed 'Incorrect syntax near .' in the batch near: ':!!copy
So I tried to simplify my testing and tried:
:!! dir
This also failed:
SQL72007: The syntax check failed 'Incorrect syntax near .' in the batch near: ':!! dir
It fails when I try:
!! dir
I am able to execute commands like:
:r .\myfile.sql
I noticed the following error occurs whenever I do :!!,
72006: Fatal scripting error: Command Execute is not supported.
Why is command execute (:!!) not supported?!
This can, of course, only truly be answered by the designers of SSDT, but you can make an educated guess: it would compromise system security too much. It would allow you to run arbitrary commands on the database server's OS, under the account of the user that runs SQL Server itself.
The sqlcmd.exe command allows one to disable the :!! and :ed commands, using the -X command line option, for the same reason.
If you really need to run OS commands in your script, you could still fallback to xp_cmdshell, which of course also has issues, but at least the server's administrator can decide if it should be allowed.
There are a lot more SQLCMD syntax options, that SSDT pre- or post deployment scripts do not support (for example :connect or :on error, it seems). So, another explanation would be, that for the purpose of deployment scripts the :r and :setvar commands were considered sufficient.