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

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.

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"

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

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

Using BCP utility from R

Is it possible to use the BCP utility in R?
I'm currently using the RODBC package to read from a remote SQL server, but am experiencing slow transfer of data from sqlFetch() which could be alleviated with the use of BCP.
Yes it is possible.
First make sure you can run the BCP utility everywhere by including the path in the Environment Variables of Windows or you can use the full file path.
Then run:
shell("bcp dbName.dbo.tableName in mydata.csv -F 2 -S sqlSrvr -T -f bcp.fmt")
This should be exactly as if you were running it from the cmd prompt.
The hard part is setting up your data so it matches the format file.

Can you write sql commands using a .bat file?

I'm designing a process to create a list of fuzzy duplicates for my colleagues. I have automated most of the process and have used a .bat file to open sqlite. However, I can now find no other way to read the code other than to manually type:
.read file_name.sql
Into command prompt. Is there a way I could type open and read the file from notepad with the commands prewritten, like a .bat file. For example:
cd sqlite -- enter directory with sqlite3 inside of it. DOS command
sqlite3 --to open the sqlite3 application DOS command
.read file_name.sql -- SQLite command
Thanks in advance, sorry if the question is trivial.
You can give a command as parameter to the sqlite3 tool:
sqlite3 mydatabasefile ".read file_name.sql"
You can find information on what you're looking to do with the SQLCMD tool. It allows SQL to be run from the command line.

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