How to run sql queries in SQL Developer or TOAD and extract result using bat file - sql

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

Related

How do I import a sql data file (the content of file are INSERT queries) into SQL Server?

I have a .sql file with a content of insert queries for all tables of my database.
This file is 12 GB in size. I tried to open it with Notepad++, SQL Server Management Studio, and also with chrome browser, but the file is very long. I cant' open it.
How can I import it into my database directly without opening the file and execute the queries?
What is the proper way to do this?
you need to use sqlcmd , the documentation has a full explanation of how to use it but here is a quick sample how to use it, you need to install sqlcmd utility first then open up your cmd command prompt and type the command like this :
sqlcmd -S DBSERVER\TESTINSTANCE -d DATABASENAME -U USERNAME -P PASSWORD -i "D:/InsertData.sql"

Unable to run .sql file in SQL Server

I have a .sql dump file 20 gb and I am trying to run it on Mysql workbench using run script and after successful execution, using SSMA I'll migrate the data from Mysql workbench to SQL Server. I have migrated the data this way many times successfully however for 20 gb file it seems very time-consuming. Please let me know if there is any alternate way to achieve this quickly. I have followed the following link:
Steps to migrate mysql tables to sql server using SSMA!
From your Title "unable to run .sql file in SSMS" and "I have a .sql dump file 20 gb" are you trying to open a 20GB .sql in SSMS? That's never going to work. SSMS is a 32bit application, so the maximum addressable memory is 2GB. If you want to run your .sql file, I suggest using sqlcmd.
Open up Powershell, and then run the command below replacing the appropriate parts:
sqlcmd -S {Server Name/ServerIP} -U {Your Login} -i {Your full path to your script}
You'll be prompted for your password and then you the file will be run. So, as an example, you might run:
sqlcmd -S svSQL2017 -U Larnu -i \\svFileServer\SQLShare\Scripts\BigBatchFile.sql
If you are using integrated security, then don't pass the -U parameter for the command.
Edit: This answer is no relevant to the OPs question, as they were using "SSMS" as a synonym for SQL Server, which it is not. I have left this here for the moment so the OP can review my comments, and I will likely remove this answer at a later point.

Schedule .sql file

I need to schedule a .sql file. (that executes some queries)
I have sql express so I can not use SQL Agent because I have SQL Express so I wish to use Windows scheduler.
But the schedule just opens the file without execute it.
How can I solve it?
You'll have to call SQLCMD.EXE together with the related parameter.
For example:
SQLCMD.EXE -S .\sqlexpress -E -i C:\temp\myfile.sql
For a detailed explanation plese have a look at this blog post

what cmd command I can use ANSI formatting for my OSQL output text file?

I am running OSQL command in .bat file and I am running my SQL script and generating the output in a text file, however the data in the file is scattered and I want to generate the file in the same format as i can generate via SQL Server manually.
I have also tried with .rpt and .txt format but still facing the same issue.
So if anyone can suggest any command or any alternative way, so that I can generate the file same as I am generating manually through SQL server.
Any suggestions or idea is appreciated.
Thanks.
You can use some options like -s "," to separate your columns by a comma. In my following example I am executing a store procedure from command line. e.g :
osql -E -Q "SET NOCOUNT ON;EXECUTE REPORTDB.[DBO].[SP_LOAD_USERDATA] %date%" -o C:\ADELPHE\OUTPUT_FOLDER\_%date%.txt -w 700 -s","
Have a look on this manual.

How to run .sql file from a batch file?

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