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
Related
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).
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
Is there a way through which i can execute SQL query in SQL Developer/TOAD and extract the result in any format using .bat file.
I m working on a client machine so using any other software other than SQL Developer/TOAD is not a option.
Please suggest how to create a bat file for the same.
If SQL Developer is installed I venture to guess so is SQL Plus. SQL Developer is simply a graphic interface to the database connections. Try opening a command window, I assume you can since you would like to run .bat programs, and typing in sqlplus. If this comes back with version numbers and a prompt for a user name you should be able to use this for your script.
See this answer on Stack Overflow for more tips on how to run .bat programs from SQL Plus with native SQL Plus spooling.
What version of Toad? If the version of Toad you have has the Automation Designer then you can setup an action to export query results to many different formats. See my answer in this question for steps to export query results to XLS. In step #3 you can choose other formats. Your configured actions can be scheduled or executed by .bat file. Toad's help covers command line execution of these actions.
You can put the following types of sqlcmd statements into a batch file. After running the results are saved to a txt file. This example executes a SQL file already created and saves the results to a txt file.
Step 1: Create SQL file which you want to execute.
Step 2: Execute following sqlcmd command on prompt:
sqlcmd -i SQLFile.sql -S ServerLocation -E -o File.txt
If you are using username and password run following script
sqlcmd -i SQLFile.sql -S localhost -U username -P password -o File.txt
You can run something like this for SQL plus:
sqlplus user/pwd#mydb #SQLFile.sql > File.txt
I am exporting data in SQL Server 2005 with the following command but it produces nothing in SQL server 2008, no file is created although the query is executed succesfully:
execute xp_cmdshell 'bcp "SELECT * FROM MYDB.dbo.MYTABLE" queryout d:\file.csv -c -t; -S(local) -T'
What is wrong with my command?
I managed to execute the command. Solution:
I installed more than one sql server versions. And the management studio cannot find the bcp.exe because in the enviroment path, there are more than one paths for the bcp.exe. You can check this by executing bcp.exe from command prompt and it will say sqlncli.dll was not found.
So you have to put the exact path of bcp.exe to the enviroment path (or in front of other paths). So that windows can use it. Then dont forget to restart the sql server in order to change in paths takes effect. In conclusion, my command is not wrong but the path of bcp.exe is wrong.
I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?
I answered this in this other question:
You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:
sqlcmd -E -S yoursqlinstance -i backup.sql
-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.
See the sqlcmd utility:
http://msdn.microsoft.com/en-us/library/ms165702.aspx
This allows you to run sql scripts from the command line
osql:
http://www.di-mgt.com.au/osqlUtility.htm
I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.
A quick google search turns up:
sqlcmd -i <inputfile> -o <outputfile>
Hope this helps you :
sqlplus UserName/Password#DataBase #C:\sqlFolder\sqlFile.sql
P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database