sqlldr control file fileds with variable - database

infile ‘c:sandeep.txt’
truncate
into table emp
fields TERMINATED BY “|”
trailing nullcols(
column_name constant “${variable}”,
)
sqlldr run by shell script, have any method pass variable to sqlldr and make fields use what I passed to it ?

How about this:
#!/bin/sh
#-- setup
col_name=MY_COL_NAME
#-- create the control file. Have to escape the double-quotes
rm myloader.ctl 2>/dev/null
echo infile ‘c:sandeep.txt’ >> myloader.ctl
echo truncate >> myloader.ctl
echo into table emp >> myloader.ctl
echo fields TERMINATED BY \"\|\" >> myloader.ctl
echo "trailing nullcols(" >> myloader.ctl
echo column_name constant \"${col_name}\", >> myloader.ctl
echo ")" >> myloader.ctl
#
#-- check the results
cat myloader.ctl
#-- run sqlldr
sqlldr control=myloader.ctl

Related

Returning table fro multiple select UNION put in an array

I want to use filtered data from a sql script and use it to run in the upgrade in DB using the filtered list to upgrade.
1) I have a SQL file which is changing daily and it looks like the following:
select
'OBJECT1' object_name,
'PACKAGE BODY' object_type,
'999999' target_vers,
(Select statement which finds current version),
Decode (decode which comperes target version and current version) status
from DUAL union
select
(the same 5 columns)
..
...
..
It could reach 100+ selects
2) I want to use only the table entries which target version>current version (sometimes target version could be < current version)
3) The object name corresponds to .code directory where the corresponding sql, view, pks is found.
4) After the filtered step I want to run these sql file in db server (looks like it would need to be done in loop).
5) since I am new to cmd/batch not sure how to do this.
for /f %%k in ('sqlplus -s %LOGIN%/%LOGIN%#%DBSERVER% #%SCRIPTPATH%\CHECK_CODE.sql') do (
set theValue=%%k
)
The code above runs the sql file mentioned above,
spool new.txt
select ......
spool off
/
exit
This returns the below txt file
OBJECT_NAME OBJECT_TYPE TARGE
--------------------------- ------------ -----
INSTALLED_VERS
--------------------------------------------------------------------------------
STATUS
-------------------------
OBJECT_NAME PACKAGE BODY 72021
72021
--- INSTALLED ---
Now I would want to do a loop only through the needed objects to update the DB.
for /f %%a in ('List of entries which need upgrade')
do (
sqlplus -s %LOGIN%/%LOGIN%#%DBSERVER% #%%a.[object_name].[object extension]
)
The Question is how can I get the filtered data from the first SQL script into an array in CMD which would then use these to run the SQL files (which are identicall to object_name)
Ended up doing something like:
DEL UPGRADE_LOG.TXT
set SCRIPTPATH=%CD%
set BAKNAME=LGH_670
set SCHEMAPATH=X:\HF\QA\Builds\Build Area\LGH\Linedata Global Hedge 6.7.0\QA1\LGH\Oracle_Objects
set LOGIN=LGH_670
set APPSERVER=LN1QAAPP11
set DBSERVER=LN1QADB11_FM.WORLD
set num=0
ECHO ********************************* Loging is starting before services being stopped *************************************** >> UPGRADE_LOG.TXT
ECHO *** Stopping services >> UPGRADE_LOG.TXT
NET STOP %LOGIN% >> UPGRADE_LOG.TXT
ECHO *** Checking V_MQ >> UPGRADE_LOG.TXT
for /f %%k in ('sqlplus -s %LOGIN%/%LOGIN%#%DBSERVER% #%SCRIPTPATH%\help_scripts\MESSEGE_QUEUE.sql') do (set theValue=%%k)
cat V_MQ.txt | sed 's/[[:space:]]//g' >> V_MQ_2.txt
find /c "norowsselected" V_MQ_2.txt
if %errorlevel% equ 1 goto queufull
goto queueempty
:queueempty
del V_MQ_2.txt
del V_MQ.txt
for /f %%k in ('sqlplus -s %LOGIN%/%LOGIN%#%DBSERVER% #%SCRIPTPATH%\help_scripts\USER_OBJECTS.sql') do (set theValue=%%k)
cat USER_OBJECTS.txt | sed 's/[[:space:]]//g' >> USER_OBJECTS_2.txt
find /c "norowsselected" USER_OBJECTS_2.txt
if %errorlevel% equ 1 goto queufull
goto validobjects
:validobjects
del USER_OBJECTS_2.txt
del USER_OBJECTS.txt
ECHO ***Coppying data from "%SCHEMAPATH%" >> UPGRADE_LOG.TXT
xcopy /s "%SCHEMAPATH%\*.*" *.* /Y
ECHO *** Creating Upgrade scripts and cleaning up the folder >> UPGRADE_LOG.TXT
call %SCRIPTPATH%\replace_string.bat V_WHAT V_WHAT_BAK CHECK_CODE.sql
REM ################### Here CHECK_CODE would be checked ##############################
echo select Object_name, object_type from ( > CHECK_CODE.sql.new
type CHECK_CODE.sql >> CHECK_CODE.sql.new
type CHECK_CODE.sql.new > CHECK_CODE.sql
del CHECK_CODE.sql.new
echo spool CODE_CHECK.txt > CHECK_CODE.sql.new
type CHECK_CODE.sql >>CHECK_CODE.sql.new
type CHECK_CODE.sql.new > CHECK_CODE.sql
del CHECK_CODE.sql.new
echo set headsep off > CHECK_CODE.sql.new
type CHECK_CODE.sql >>CHECK_CODE.sql.new
type CHECK_CODE.sql.new > CHECK_CODE.sql
del CHECK_CODE.sql.new
echo set pagesize 0 > CHECK_CODE.sql.new
type CHECK_CODE.sql >>CHECK_CODE.sql.new
type CHECK_CODE.sql.new > CHECK_CODE.sql
del CHECK_CODE.sql.new
echo create table V_WHAT_BAK as select * from V_WHAT; > CHECK_CODE.sql.new
type CHECK_CODE.sql >>CHECK_CODE.sql.new
type CHECK_CODE.sql.new > CHECK_CODE.sql
del CHECK_CODE.sql.new
REM Adding last lines in the system
echo ) where status='### NEEDS UPGRADE ###'; >> CHECK_CODE.sql
echo spool off >> CHECK_CODE.sql
echo DROP TABLE v_what_bak; >> CHECK_CODE.sql
echo / >> CHECK_CODE.sql
echo exit >> CHECK_CODE.sql
REM Here We need to run in the script for alternative check.
for /f %%k in ('sqlplus -s %LOGIN%/%LOGIN%#%DBSERVER% #%SCRIPTPATH%\CHECK_CODE.sql') do (set theValue=%%k)

SQLCMD with 1 return value

I am trying to return a single value from a sqlcmd query used in a bat file.
It seems that i have some errors with the syntax used in my sqlcmd query. I am very new to coding in cmd, any guidance would help. I am using this query on a server.
#ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
CD %~dp0
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF (%CFIGTYPE% == 'TEST1' ){
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
do set ColumnVar=%%a
echo %ColumnVar%
CMD \K
IF (%ColumnVar% == 'Y'){
Echo WOHOOOO
cmd /k
}
Else{
ECHO NOOOOOO
CMD /K
}
My code just closes after it reaches the sqlcmd portion.
Revised code in light of comments:
(does not solve problem)
#ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF "%CFIGTYPE%"=="TEST1" (
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
do set ColumnVar=%%a
echo %ColumnVar%
IF "%ColumnVar%"=="Y" (
Echo WOHOOOO
cmd /k
)
Else (
ECHO NOOOOOO
CMD /K
)
)
New revision:
#ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Database Server Name
Echo [2] Configuration Type
echo ---------------------------------------------------------
SET /p DBSERVER="Enter [1]: "
SET /p CFIGTYPE="Enter [2]: "
echo Deploying...
ECHO %CFIGTYPE%
::-----------------------------------------------TEST1----------------------------------------
IF "%CFIGTYPE%"=="TEST1" (
for /f %%a in ('sqlcmd -S %DBServer% -v DBServer="%DBServer%" -Q "SET NOCOUNT ON; select ConfigValue FROM SERVICE.dbo.configuration WHERE ConfigOption = 'TEST1' "')
IF "%%a"=="Y" (
Echo WOHOOOO
PAUSE
) Else (
ECHO NOOOOOO
PAUSE
)
)
I didnt manage to find a solution that allows me to set a value to a variable from a sql query. Instead, i have output the results to a file and read the results from there and then delete the file. Here goes the code:
#ECHO off
echo _________________________________________________________
Echo The following inputs are required:
Echo [1] Myserver
echo ---------------------------------------------------------
CD %~dp0
SET /p DBSERVER="Enter [1]: "
:: Get Configuration Type
sqlcmd -S %DBServer% -E -i test.sql -b -v TYPE="CFIGTYPE" -h -1 >result.txt
set /p CFIGTYPE=<result.txt
call :Trimspaces CFIGTYPE %CFIGTYPE%
del "result.txt"
:: ------------------------------------------------ FUNCTIONS--------------------------------------------------------------------------------------------------------------
:trimSpaces2 retval string -- trims spaces around string and assigns result to variable
:: -- retvar [out] variable name to store the result in
:: -- string [in] string to trim, must not be in quotes
for /f "tokens=1*" %%A in ("%*") do set "%%A=%%B"
EXIT /b
:trimSpaces varref -- trims spaces around string variable
:: -- varref [in,out] - variable to be trimmed
call call:trimSpaces2 %~1 %%%~1%%
EXIT /b
Since the output file would contain empty i have included a function taken from
https://www.dostips.com/DtTipsStringOperations.php
That would eliminate the empty spaces when reading the file.
I have an extra test.sql which contains the following code:
SET NOCOUNT ON;
select configvalue FROM Service.dbo.Configuration WHERE ConfigOption = '$(type)'

GPG unattended encryption not giving output

I have created a script which will encrypt a file using GPG and upload it to a target.
The script is as follows:
cd C:\test\outfeeds\
echo ---------- >> C:\test\log.txt
date /T >> C:\test\log.txt
echo %time% >> C:\test\log.txt
echo %USERNAME% >> C:\test\log.txt
echo ---------- >> C:\test\log.txt
echo --- Encrypting Files --- >> C:\test\log.txt
for %%a in (*.txt) do (
gpg --allow-weak-digest-algos -o %%a.pgp -e -r "testkey" %%a >> C:\test\log.txt
)
echo --- Transferring Files --- >> C:\test\log.txt
sftpc -profile="C:\test\FTP\test_profile.bscp" -hostKeyFile="C:\test\FTP\Host_Key_Export" -unat=y -cmd="put *.pgp inbound/ap/" >> C:\test\log.txt
del *.pgp
move *.txt C:\test\feedarchive\
The logs show nothing for the gpg call so when i run it unattended I can see it runs under a user named testmachine$. This results in no file being available for the sftpc call (so I assume this file isn't getting made).
When I run it manually, it seems to work fine, logging nothing for the gpg call, but creating the file and uploading it successfully.
Is there a way to return what's going wrong on this call, or something obvious I am doing wrong?

Batch execution - System cannot find the file specified

My batch file is running the following code:
[...]
copy nul %BaseDir%\bin\MyIniFile.ini
echo [OEMINFO] >> %BaseDir%\bin\MyIniFile.ini
echo OEMTextColor= >> %BaseDir%\bin\MyIniFile.ini
echo OEMBackGroundColor= >> %BaseDir%\bin\MyIniFile.ini
echo OEMNoStartupLogos=T >> %BaseDir%\bin\MyIniFile.ini
echo OEMInfoGreetings= >> %BaseDir%\bin\MyIniFile.ini
echo OEMIcon=<BD>\config\Default\cad.ico >> %BaseDir%\bin\MyIniFile.ini
[...]
I always get the "system cannot find the file specified" error, but the ini file is correctly created with all the entries except for the last one (OEMIcon=\config\BormGroupERP\Default\cadt.ico).
At first i tried it with
echo "OEMIcon=<BD>\config\Default\cad.ico" >> %BaseDir%\bin\MyIniFile.ini
which worked (no error message) but then I have the quotation marks in my ini file which is bad...
Also
echo [OEMIcon=<BD>\config\Default\cad.ico] >> %BaseDir%\bin\MyIniFile.ini
does not work (same error again).
I also tried a little workaraound by storing the string in a variable (with quotationmarks) and then remove the quotation marks.
set OemIconData="OEMIcon=<BD>\config\Default\cad.ico"
OemIconData=%OemIconData:~1,-1%
But the same error occured just when i try to remove the quotationmarks.
I'm out of ideas now. Why does that error occure? How can I prevent it?
< and > are special characters that have their own reserved use.
< is an input re-directer, which sends everything after it into the command before itself.
> is an output re-directer, this sends everything before it to the location/ file/ command after itself.
Currently, cmd.exe executes as so:
Gets the command result of BD>\config\Default\cad.ico >> %BaseDir%\bin\MyIniFile.ini
Echo echo OEMIcon=<BD into \config\Default\cad.ico >> %BaseDir%\bin\MyIniFile.ini
We will need to escape the special character so cmd ignores it's usage, by adding a ^ in front of it. Your command should be:
echo OEMIcon=^<BD^>\config\Default\cad.ico >> %BaseDir%\bin\MyIniFile.ini

Include findstr output which contains SP name in sqlcmd

How should i include the output of a findstr which retrieves me the name of the stored proc in sqlcmd?
#ECHO OFF
FOR %%f IN ( *.sql) DO (
var xyz = echo %%f | findstr "^test*"
sqlcmd -d databaseX -S server -e -i xyz
)
where xyz will evaluate to testStoredProc1.sql, testStoredProc2.sql and so on.
These stored procs have update/insert and select statement
How can i capture the value of xyz and pipe it to sqlcmd?
#echo off
for %%f in (test*.sql) do (
sqlcmd -d databaseX -S server -e -i %%f
)
NOT TESTED!
For further information check FOR /F , SET ,DelayedExpansion

Resources