I have a requirement to create a SQL Job that exports a query to a Pipe / Vertical bar delimited file (|) and save it on a network drive in either *.txt or *.csv format. Right now, I am just trying to get this to work inside SSMS, calling the BCP command and exporting the stored procedure in the proper format to the network location, but not able to get it working.
I have been researching this and there are two methods for this.
Use the export data wizard to create a job and schedule that to run. But this method, if we need to make changes, I believe we cannot change the SSIS package that is created so we lose flexibility
Use the BCP command to export the file.
I greatly prefer to use option #2, the BCP command, but I am having problems. I just cannot seem to get the syntax correct and hoping someone could show me what I am missing:
This is my command:
Exec master..xp_cmdshell 'bcp EXEC [dbo].[usp_Report_1123] ''7786'' -t| out \\networkDrive\Reports\REPORT_1123\report1123.csv -T'
But I get the following messages:
output
'out' is not recognized as an internal or external command,
operable program or batch file.
NULL
The stored procedure does work and returns data. The network path, if I enter it into my computer, finds the path. But I am not sure what I am missing and hoping someone could help.
I have a Python project where I use sqlite3 to save data.
I want to do a backup of the database ( I am really worried about datalock, cause my software will be used by like 10 peoples which will have to access to my database to write or read, and without luck, someone will try to access to the database at the bad moment, even if I will increase timeout)
If I do it manually with the Windows cmd, there is no problem.
v:
cd V:\directory\
sqlite3 mydatabase.db"
.backup backup_db.db
I try to do a batch file which will be call every hours by my python software to do it automatically.
I applied the same commands in it.
The batch file is launched, but the process stop after the opening of the database.
The dot command is not executed.
Where is my mistake?
Batch files do not work the way you think they do.
You currently think that after the line sqlite3 "mydatabase.db" you are in some kind of "database mode" and all the following lines in the batch file are passed to the sqlite3 process.
That's not the case.
Every line in a batch file is executed after the previous like has finished running.
The line sqlite3 "mydatabase.db" starts the sqlite3 process... and then waits until this process exits. Which never happens unless you do it manually by pressing a key.
And after that cmd.exe will try to execute the command .backup backup_db.db, but since that's not a command cmd.exe understands, it will fail with an error ("'.backup' is not recognized as an internal or external command, operable program or batch file.").
What you really want to do is create a script file and pass it to for SQLite for processing. This can be done by
redirecting a file into the sqlite3 process. Assume that create_backup.txt contains the commands to create a backup:
sqlite3 "mydatabase.db" < create_backup.txt
piping the file into the sqlite3 process, e.g. using type:
type create_backup.txt | sqlite3 "mydatabase.db"
alternatively you can use echo to output a command:
echo .backup backup_db.db | sqlite3 "mydatabase.db"
Obviously the echo method is easier for one-liners whereas the input redirect with < or writing out a file with type are easier to for more complex, multi-line operations.
Using the -init parameter of sqlite3.exe is another option you can try. Check the SQLite documentation.
The syntax for the sqlite3 executable is sqlite3 [db_file] [command]. So, in your batch file, you should include .backup on the same line.
v:
cd V:\directory\
sqlite3 mydatabase.db ".backup backup_db.db"
I'm running a simple batch file to execute a SQL statement.
The batch file:
cd /D C:\sqlcl\bin
sql username/password#host:port:SID #C:\scripts\statement.sql
exit
The SQL statement runs perfectly. It spools the results of the query to a CSV file and returns to the SQL> prompt.
The issue is the batch file doesn't go to the next line to exit SQL and the CMD window. No matter what command I test, the batch file does not go to the 3rd line.
Is there something I need to add to *.sql file to force it to end? Am I missing something with using exit? I've tried QUIT, HOST to return to CMD, ECHO to print something but not matter the command, it never goes to that line but does return to the SQL prompt.
Adjust your .sql file to add an exit at the end such as this.
set feedback off
set head off
set sqlformat csv
spool emp.csv
select * from emp;
exit
Run as you normally are
$ sql klrice/klrice #spool
resulting file is a csv as expected.
$ more emp.csv
7369,"SMITH","CLERK",7902,17-DEC-80,800,,20,5555555555554444
7499,"ALLEN","SALESMAN",7698,20-FEB-81,1600,300,30,4929043445510803
7521,"WARD","SALESMAN",7698,22-FEB-81,1250,500,30,6011823599867990
7566,"JONES","MANAGER",7839,02-APR-81,2975,,20,375055626849864
7654,"MARTIN","SALESMAN",7698,28-SEP-81,1250,1400,30,4929727676353442
7698,"BLAKE","MANAGER",7839,01-MAY-81,2850,,30,5262511577814781
7782,"CLARK","MANAGER",7839,09-JUN-81,2450,,10,6011983140249807
7788,"SCOTT","ANALYST",7566,09-DEC-82,3000,,20,343764091280047
7839,"KING","PRESIDENT",,17-NOV-81,5000,,10,5186144047197497
7844,"TURNER","SALESMAN",7698,08-SEP-81,1500,0,30,6011331487563093
7876,"ADAMS","CLERK",7788,12-JAN-83,1100,,20,378775397941460
7900,"JAMES","CLERK",7698,03-DEC-81,950,,30,4916225758678451
7902,"FORD","ANALYST",7566,03-DEC-81,3000,,20,378355660338882
7934,"MILLER","CLERK",7782,23-JAN-82,1300,,10,34567
$
An interesting issue was causing this problem.
In the original SQL file I had exported from SQL Developer, I had not included "exit" at the end. I opened it in Notepad and added "exit". Every time, I looked at the SQL file, I opened it in Notepad and "exit" was there.
I just opened the SQL file in Notepad+ and "exit" was NOT there. I added it and the batch file now works fine.
I have no idea why this issue happens.
i have a oracle sqldeveloper file.
when i run it by using a batch file, if it encounter an error, the cmd will surely display the error.
what i want is, is it possible to take the cmd error as an input to trigger out my custom made error message??
if in cmd displaying any error for example:
Parameter not found
my custom-made error message in batch file will triggered out this error message in the cmd:
You Got An Error!
any help and suggestions are appreciated..
Thanks in advance!
You can redirect the output (both stdout and stderr) to a text file (or to nul if you don't care about the output), and check the value of `%ERRORLEVEL% after your script has been executed. If it is not 0, then display your own error message.
To redirect both stdout and stderr:
command parameters 1>>c:\log.txt 2>&1
I recommend using SQL*Plus instead of SQL Developer. You can add WHENEVER SQLERROR EXIT FAILURE to the top of the script (or EXIT SQL.SQLCODE if you are interested in the specific error code). Then the calling batch file will receive the error code in %ERRORLEVEL% and you can take action appropriately. There is also WHENEVER OSERROR EXIT FAILURE if your SQL script interfaces with the OS with the potential of failure there.
I don't think SQL*Plus uses stderr, so I don't think you need to worry about redirecting 2>&1.
I have a stored procedure in Sybase that uses reorg rebuild statement in a loop for all the tables in my database. What I want to do is to suppress the reorg rebuild sysmessages for tables that succedeed the procedure and only to print the tables that were locked etc...thus the problematic ones....The thing is that I did not succeed to find out anything to use in manual or in any workshops...dow you have any idea?
Thanks in advance !!!!!
If you are running the SQL with isql at a command prompt, you can always capture the output in a text file and filter it out with other tools.
Create a script to run the SQL in isql and then use a script that calls a text processing tool (awk,sed,...) to only find the lines of interest.
Here is an example from a windows batch file with a regex that removes lines that start with a space (i.e. Rows Effected messages)
isql -SDBDEV1 -DMyDbName -U%DBLOG% -P%DBPWD% -iLoadBatchStats.sql -o%TEMP%\LoadBatchStats.log
type %TEMP%\LoadBatchStats.log | gawk "/^[ ]/{print $0}" >>%TEMP%\LoadBatchSummary.log