Teradata - run a file/script at Unix Linux command prompt - unix

I have the client/TTU installed on Unix box for Teradata.
If I do the following, it works. Where "..." is Teradata BTEQ normal output and once the following is done, I'm back at the prompt.
$ bteq
...
....
. logon dbname/dbuser,dbpassword
SELECT DATE, TIME;
.LOGOFF;
.QUIT;
..
...
$
Now, lets say I put the following lines in a file called "testtd.bteq"
. logon dbname/dbuser,$dbpassword
SELECT DATE, TIME;
.LOGOFF;
.QUIT;
What I want now is ... how can I run this script (.bteq) at Unix $ prompt ???
I tried the following methods so far, but they didn't work, may be Im missing anything:
1. bteq < /path/to/testtd.bteq > testtd.log
2. bteq <
.run /path/to/testtd.bteq
HereDocEndsHere
Any ideas? DO I HAVE to provide ". logon dbname/dbuser,dbpassword" FIRST, if I'm using the HereDocument way?
Running bteq command on $ prompt doesn't even give me any HELP/options that I can use, like other commands.
i.e.
cmd -u user -p password -f file etc...

The best practice I'm aware of is
store your teradata credentials in a ~/.tdlogon file
create a script that contains your bteq call with all the stuff it needs.
E.g., create a file bteqScript.sh with
/* define helper variables, e.g.... */
export ARCHIVEDIR=~/data
export DATAFILE=dataOutput1.txt
bteq <<EOF
.run file=$HOME/.tdlogon
.export data file=${ARCHIVEDIR}|${DATAFILE}
/* sql code on next line(s) */
select
'foo' as "bar"
;
.export reset
EOF
Note that .run file=... executes the .logon command with your credentials, stored elsewhere.
Kudos to Alex Hasha for the bteq script.

PS - It works via method 1 -- when I hard code the password in the script file for LOGON command.
I wanted to do the same via exporting a variable called "dbpassword"
i.e.
$ export dbpassword=xyxyxyxyx
and
then, inside the script file, i can use "$dbpassword" ... in the LOGON command.. somehow export is not exporting the var within .bteq logon command.

Related

Teradata BTEQ: disable logon prompt

When i run a teradata bteq in the CMD shell -
A little logon prompt screen pops up.
When i press enter the bteq runs.
Is there a way to disable this popup screen?
Searching the internet yields that entering a logonprompt off should solve the problem.
Like so:
.SET LOGONPROMPT OFF
.LOGON my_server
-- rest of the bteq script...
.QUIT
.LOGOFF
I use the following shell command to run the bteq:
bteq < myscript.sql > log.txt
Could you please help me get rid of the logon popup screen?
You can pass the fully qualified logon string to get rid of the prompt.
.LOGON my_server/user,password
If you still want to go with only the server details in .LOGON. Along with .SET LOGONPROMPT OFF, set the environment variable GUILOGON as NO.
In CMD: setx GUILOGON NO
Snippet from TD Documentation:
Note that setting LOGONPROMPT to OFF is sometimes not going to be
sufficient for suppressing all unnecessary prompts when using Windows
BTEQ. You may also need to instruct CLI to suppress its generation of
what is known as its GUILOGON dialog box.
This can be accomplished by setting the environment variable GUILOGON
to NO.

How to hide Logon Password in Teradata MLoad Script

I have written a Multiload script to Load Data in TeraData Database and the commands in the script is like:
.LOGTABLE Employee_log;
.LOGON 192.168.1.1/dbc,dbc;
.BEGIN MLOAD TABLES Employee_Stg;
.LAYOUT Employee;
.FIELD in_EmployeeNo * VARCHAR(10);
.FIELD in_FirstName * VARCHAR(30); ....
But the password is clearly visible in the script. Is there a option to secure the password or any alternate way/command to logon and then run the script.
You can create a logon file and run it in your MLOAD script using the following command
.RUN FILE logonfile.txt
In the logon file you can provide the statement that you used in your script .LOGON 192.168.1.1/dbc,dbc;
Restrict access to logonfile.txt, though only the user can read it
chmod go-rwx logonfile.txt
or use tdwallet
.LOGON 192.168.1.1/dbc,$tdwallet(dbc)
tdwallet keeps the entries safely away, only access via logon command.
There is no function to get an entry in cleartext.

BTEQ: Save Teradata error in logfile

My goal is to automate a deployment of SQL scripts to Teradata via BTEQ. So far my script is working. However, I would like to generate a log file where possible failures are captured.
.LOGON tdserver/username,pw
.EXPORT file=\logfile.txt;
.run file = \Desktop\test\test.sql;
.LOGOFF
.EXIT
My SQL script will create a VIEW. When this view for example already exists I see an error in the BTEQ command window:
*** Failure 3804 View 'ViewName' already exists.
I would like to have this TD Message in my log file. I tried several tings, have been looking for 3 hours but unfortunately without success.
You may want to experiment using .SET ERROROUT STDERR which re-routes the error stream to the STDERR output file instead of the default action of routing the error stream to STDOUT.
There is more information in the BTEQ manual under Chapter 5 - BTEQ Commands.
Save all your script written above as a text file and create a batch file that generates a log after running the script:
echo off
bteq < script.txt > script.log 2>&1
#echo off goto end
:end
#echo exit
Errors will be recorded in this way.

Error while running a .sh script via QProcess

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}

How to run sql script in geany?

I am able to run my ex1.sql script from the command line, but in geany the follow error pops up:
./geany_run_script.sh: 5: ./geany_run_script.sh: ./ex1: not found
Does anyone have an idea?
Geany cannot execute sql directly. You have to configure the Build Commands from inside the build menu to do this e.g. to psql -f %f to run sql script with psql client.
For me to work I did this
modify the filetypes.sql
this way:
[build-menu]
EX_00_LB=_Exécuter
EX_00_CM=/opt/lampp/bin/mysql -u root < %f
EX_00_WD=
/opt/lampp/bin/mysql = is where mysql is install on my computer
< %f = allow the sql to be executed
dont forget to specify whitch DB you want to work with in your code
so put a line like use Mydatabase;
at the top.

Resources