I have some insert statements in a .sql file.
I want to execute the insert statements via sqlcmd and tried to do it like this:
sqlcmd -S (localdb)\MSSQLLocalDB -i C:\BacklogItems\15298\dbo.ak_funktion_typ.Table.sql
Unfortunately, I get the following error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near 'S'.
What could be the problem with the -S?
According to https://www.mssqltips.com/sqlservertip/4924/execute-sql-server-script-files-with-the-sqlcmd-utility/ the I can provide the server name via this parameter...
I've made sure that the query is in the SQLCMD Mode.
Thanks in advance for any tips
The reason you are getting that error is because you need to execute sqlcmd in a Windows Command Prompt environment, not in a SQL editor environment such as SSMS. sqlcmd is a separate executable (.exe) utility which has some equivalence to a GUI such as SSMS, in that it is another type of client program for communicating with the database server. It is not a tool which is used within SSMS itself (or any other SQL client).
Related
The following PSQL command works on Mac/Linux:
psql "dbname='public' user='myuser' password='XXXXXX' host='some.host.com' port='5432'" --file=permissions.sql
But when I try to use the same command on windows, it will login but not execute permissions.sql. I tried changing permissions.sql to \dbbackup\permissions.sql so that I would specify the the full path to the sql script. That didn't appear to help.
When I run the command in Linux, psql logs into the db and executes the script. The same command logs me in to the db but doesn't execute. I just get a command prompt like this: DATABASENAME=>
Then I have to execute \q to logout.
Any suggestions are greatly appreciated.
I have .sql file which contains millions of Insert commands, and they are having insert statements to be inserted into different tables. When I am executing by opening in SQL SERVER MANAGEMENT it says
Insufficient memory to continue the execution of program
You can save the SQL in a file and execute it from the command line using sqlcmd. For example:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Please note: If your instance is a default instance (i.e. the instance name is MSSQLSERVER), then do not specify it as part of the sqlcmd parameters. To connect to the default instance, simply specify the server name. For example:
sqlcmd -S myServer -i C:\myScript.sql
I would suggest you try running the .sql from the command line (sqlcmd.exe) instead of loading it in SSMS.
SQLCMD - MSDN Link
I am trying to run a .sql file in SQL Server Management Studio using this command
EXEC xp_cmdshell sqlcmd -s '127.0.0.1' -d MyDB -i 'C:\Data\ProcessedSQL\ReversalFile1.sql'
but I am getting an error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'
Can someone assist me please?
Update Edit:
To be clear, I am only starting to use SQL Server.
I have several sql files in one folder and I was hoping to run a query window in SSMS to run several sql files one after another as follows:
Execute file1
Execute file2
Execute file3
The Files are being generated out of another system by a DBA.
Use SSMS in SQLCMD mode to run an external SQL file:
:R Pathtoyourfileinthesqlserver
https://msdn.microsoft.com/en-us/library/ms174187.aspx
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.
Is there anyway to run a stored proc from cmd.exe ?
p.s.
I know how to create exec file c# and to run it.
I'm asking without any code :
Just me and cmd.exe.
edit
me and cmd.exe : meaning I don't want to write any code. Internal SQL Server help exe files of sql is fine !
sorry for not clarifying this !
Try using the SQLCMD Utility :
An example:
sqlcmd -E -S server_name -d database_name -Q "EXEC schema.storedprocedure parameter01, parameter02"
If you'd like to explore other options I'd like to share this link:
Command Prompt Utilities: Applicable for SQL Server 2005
For sql server 2005+ use its SQLCMD utility, as #Nonim answered
for versions prior to 2005 use its OSQL utility, its usage is similar to SQLCMD
Wrap it into the .cmd file and you're in! No needs to write something, just click'n'go! 8-)
C# files are not executable. But the compiled program from them is.
CMD is not C# parser neither SQL client. From the CMD you call a program that serves a file. You and CMD can navigate to drive A: to execute file there, to delete files and so on.
CMD is emulated environment of DOS, which is also OS - you need a program to execute the SQL queries. See this (Sqlcmd program of SQL Server):
http://msdn.microsoft.com/en-us/library/ms165702.aspx