Search code examples
sqlsql-servert-sqlsqlcmdsql-server-2017

Execute sql in a file via sqlcmd


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


Solution

  • 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).