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

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

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.

SQL log in from Cron job

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
%%

How to Insert million of Insert Command having in .sql file

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

Teradata client on Unix Solaris

I deploy some .bteq and .sql scripts on a TERADATA database. For doing this, I use a client on my desktop called BTEQWin version 13.10.0.03.
I get the .bteq/.sql from a version control like pvcs/svn etc and all I do once the files are in my workspace folder (from Version control tool), to just drag and drop the files from Windows browser to BTEQWin client (which I connect to a database prior to drag/drop for running those scripts).
Now, I have to automate this whole process in UNIX.
I have written a SHELL KSH/BASH script which is getting all the .bteq/.sql from a TAG/LABEL in the version control tool to a given UNIX folder. Now, all I need to do is the pass these files one by one (i'll take care of the order) to Teradata client.
My ?
- what client do I need to tell Unix admin team to install on Unix server - so that I can run something like below:
someTeraDataCommand -u username -p password -h hostname -d database -f filenametoexectue | tee output_filename.log
Where, someTeraDataCommand is the client / executable - which will let me run Teradata scripts (like I was doing using BTEQWin on my desktop - GUI session). Other parameters can be username, password, which database to connect on what server and which file to run or make that file passed to the command using "<" operator at command line.
Any idea?
- What client ?
Assuming the complete Teradata Tools and Utilities package is installed on your UNIX server (which will have the connectivity tools to talk to Teradata), you should have access to bteq from the command line. Something like this:
bteq < script_file > output_file
Your script file should contain a .LOGON statement to establish the connection:
.LOGON yourTDPID/your_account,your_pw
You might also need to use other commands to set your default database or non-default session values.
Another option would be to combine the SQL and call to BTEQ in a Korn shell script:
#!/usr/bin/ksh
##############
SHELL_NAME=`basename $0`
PRG_NAME=`basename $(SHELL_NAME} .ksh`
LOG_FILE=${PRG_NAME}.log
OUT_FILE=${PRG_NAME}.out
#
bteq <<EOBTQ > ${LOG_FILE} 2>$1
.LOGON {TDPID}/{USERID},{PWD};
--.RUN file=${LOGON}
/* Add your SQL/BTEQ commands here */
.QUIT 0;
EOBTQ
Edit
The double hyphen indicates a single line comment. Typically in a UNIX script you do not leave your password in plain text of a KSH script. The .RUN command would reference a text file in a barely sufficient secure location containing the .LOGON {TPDID}/{USERID},{PWD}; command.
The .RUN command in BTEQ allows you to reference another text file containing a series of valid BTEQ commands that you want to run in the current BTEQ script.
Easiest way is to setup the Solaris TTU, is to request root sudo, and run an interactive installation into defaults as a root. That would cure all client issues.

Vaccum full and Reindex Heroku database

I want to perform a full vacuum and reindex on my database for my app hosted on Heroku.
I can't work out how to do it via the heroku command line remotely.
I can do it on my local Mac osx machine via the below commands in terminal...
psql database_name
>> vaccuum full;
>> \q
reindex database database_name
How can i perform a full vaccuum and reindex all my tables for my app on Heroku?
If possible I would like to do it without exporting the database.
Okay so it seems Heroku doesn't support this functionality unless you pay up. Looks like i'll have to pull the database, perform the actions and push it back upstream! Fun times.
You can use the psql interactive terminal with Heroku. From Heroku PostgreSQL:
If you have PostgreSQL installed on your system, you can open a direct psql console to your remote db:
$ heroku pg:psql
Connecting to HEROKU_POSTGRESQL_RED... done
psql (9.1.3, server 9.1.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
rd2lk8ev3jt5j50=>
You can also pass-in the parameters at the psql command-line, or from a batch file. The first statements gather necessary details for connecting to your database.
The final prompt asks for the constraint values, which will be used in the WHERE column IN() clause. Remember to single-quote if strings, and separate by comma:
#echo off
echo "Test for Passing Params to PGSQL"
SET server=localhost
SET /P server="Server [%server%]: "
SET database=amedatamodel
SET /P database="Database [%database%]: "
SET port=5432
SET /P port="Port [%port%]: "
SET username=postgres
SET /P username="Username [%username%]: "
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h %server% -U %username% -d %database% -p %port% -e -v -f cleanUp.sql
Now in your SQL code file, add the clean-up SQL, vacuum full (note the spelling). Save this as cleanUp.sql:
VACUUM FULL;
In Windows, save the whole file as a DOS BATch file (.bat), save the cleanUp.sql in the same directory, and launch the batch file. Thanks for Dave Page, of EnterpriseDB, for the original prompted script.
Also Norto, check out my other posting if you want to add parameters to your script, that can be evaluated in the SQL. Please vote it up.