SQL log in from Cron job - sql

On RHEL 7, I am trying to access a SQL table. When I run the code from terminal I can access the database and the script runs as expected. When I run the same script from crontab I get an error:
Error 6 initializing SQL*Plus
SP2-0667: Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
Here is my code:
getSequence()
{
SQLPLUS=/oracle/app/oracle/product/12c/bin/sqlplus
seq_num=$($SQLPLUS -S $OPS_DB_USER/$OPS_DB_ORACLE_PASSWD<<-EOF
set heading off
set feedback off
select Sequence.nextval from dual;
EOF)
VERSION=`printf "%07d" $seq_num`
}

If the script runs as expected. Make sure the permissions are set correctly on the script so when the cron runs it has the correct permissions to execute the script. Double check your environment variables as well. Here is an example of how I call a proc using a bash script. Hope this helps.
#!/bin/bash
sqlplus -s <<%%
$DBLOG/$DBPWD#$ORACLE_SID
exec "my_proc"();
exit
%%

Related

xp_cmdshell throwing 'null' but working in command prompt

I am using xp_cmdshell command to process csv file to a folder. My query is as below:
Exec master..xp_cmdshell 'C:\test\mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test'
In SSMS this command returns null. But in command prompt when I do:
cd C:\test
mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test
The same query is working perfectly fine. A couple of things:
xp_cmdshell is enabled
SQL Server has access to the folder
Only this xp_cmdshell command is not working. my other xp_cmdshell is working fine
Any help on how this can be fixed?
If this is run as part of a SQL Job regularly scheduled, run it as a task outside of SQL Server.
Many production DBAs won't let any process run XP_CMDSHELL given the security issues.
If the CSV is well-formed, it can be mapped into SQL Server as a table (not recommended).
It can be imported directly into SQL Server.
Lastly, many production DBAs won't install any custom process code on a production SQL Server machine. The SQL Server machine is dedicated to running only SQL Server plus the minimum extra needed for security and monitoring.

Need a basic understanding of the sqlplus command

sqlplus -s £ORA_LOGIN/£ORA_PASSWORD #./sql/getsess.sql £FEED > £FEED.lst
This is the command in a shell script. Can anyone please tell me what is this doing? because i have no knowledge about the sqlplus command
This command runs the script ./sql/getsess.sql providing the value £FEED as parameter and using the credentials £ORA_LOGIN/£ORA_PASSWORD to login. It will store the output from the call to SQLPlus in the file named £FEED.lst.
Note that SQLPlus is invoked in silent mode (parameter -S), so most of the output will not be seen in the output file.

postgres command line script not working on windows 2012 R2 Server but does work on Linux/Mac

The following PSQL command works on Mac/Linux:
psql "dbname='public' user='myuser' password='XXXXXX' host='some.host.com' port='5432'" --file=permissions.sql
But when I try to use the same command on windows, it will login but not execute permissions.sql. I tried changing permissions.sql to \dbbackup\permissions.sql so that I would specify the the full path to the sql script. That didn't appear to help.
When I run the command in Linux, psql logs into the db and executes the script. The same command logs me in to the db but doesn't execute. I just get a command prompt like this: DATABASENAME=>
Then I have to execute \q to logout.
Any suggestions are greatly appreciated.

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

Batch File Connecting to Server and execute program install on connected server

I have been searching for a resolution for days and I am stuck. I've exhausted all resource and need to resolve this. If any one out there with ideas or know how to do this, please help.
I have an SQL Server Database, on Server1. I have an application, which is installed and runs on Server2 (ServerName2) and a procedure that needs to run within an application that is installed on this server.
What I need is to execute the batch file when ever a row get inserted into a table. The SQL Server database is on Server1. The batch file starts an application and calls a script, which executes a procedure. This script needs to run inside an application which is installed on Server2. I have a trigger after insert created on the table. Now I need to some how execute this batch file. Please note, the batch file calls a program which is installed on a different server.
I thought of 2 solutions:
To use SQL Server to execute the batch file which sits on Server2. I tried using "EXEC xp_cmdshell '\\ServerName2\C:\MSTR SCRIPT FILES\Batch_File_Execute_OTB_Script.bat".
It returned 2 records, the first one "The network name cannot be found." and NULL for second one.
Create a batch file with a connection string to ServerName2, that Start the .exe program. I am able to get the batch file to start this program and run the script if I run this on ServerName2.
In summary, I need to connect from Server1 to Server2, either through SQL Server or through batch? Is this possible? If yes, please provide me with some guidance.
Once I establish a connection, do I need to call the .exe program and run my script? What is the command to trigger this from remote connection?
Here's a little code to help you connect to the SQL server and interrogate the SQL version.
#ECHO OFF
SQLCMD /? > nul 2> nul
IF ERRORLEVEL 1 ECHO.SQLCMD not found on this machine & PAUSE & GOTO :eof
SET SQLCmdLine=SQLCMD -b -w400
REM use something like the next line for NT authentication; second line for SQL authentication
REM for NT SET SQLCmdLine=%SQLCmdLine% -E -S "YourServerName"
REM for SA SET SQLCmdLine=%SQLCmdLine% -U "YourSQLUserName" -P "YourSQLPassword" -S "YourServerName"
SET Database=YourDatabaseName
REM I will use NT Authentication for this example
SET SQLCmdLine=%SQLCmdLine% -E -S "(local)"
SET TestSQLCmdLine=%SQLCmdLine% -h-1 -d "%Database%"
REM this will get the SQL version
%TestSQLCmdLine% -Q "SELECT ##VERSION" | FINDSTR /v "rows affected"
REM this will run a SQL script
%TestSQLCmdLine% -i "YourSQLScript.SQL"
PAUSE