Pass a DDL statement to a KSH script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass a DDL statement to a KSH script
# 1  
Old 08-28-2010
Pass a DDL statement to a KSH script

I need to pass a DDL statement into a ksh script & parse the statement.

What is the best way to pass a DDL statement into a KSH script.

---------- Post updated at 09:28 AM ---------- Previous update was at 07:35 AM ----------

if the name of the script is test.ksh

test.ksh "ALTER TABLE emp ADD emp_status varchar2(20);"

this is what I want to do, Can I do like this or any other way to do?
and how to handle each word of the string in the script?

Please suggest.
# 2  
Old 08-28-2010
Yes, you can pass the string in the way you have it in your example. With quotes round the whole string it will be addressable in the script as $1 (first parameter from the command line).

How you parse it really depends on what you need to do with it. A simple mechanism would be to use the set command:

Code:
set $1

The result of the set command is that each token from the string in $1 will be assigned to $1 through $n. You then can examine/use the tokens as you need to .

A small example to illustrate the set command:
Code:
#!/usr/bin/env ksh
set $1
echo $1
echo $3
echo $5

The results when t22 -- my test script -- is executed are:
Code:
$ t22 "Now, is (the) time for UNIX programmers to help;"
Now,
(the)
for

Hope this helps. If you post how you need to parse the string, there might be different/better ways to go.
# 3  
Old 08-28-2010
Thanks for replying.

Let me try to be more specific.

Here is the Scenario / requirement
When we submit the DDL to the DBA, DBA implement those DDLs to the database.
=> We need to automate the process of verifying if all the DDL are implemented properly.

1. these DDL statement could be the input string for the Script.
2. Extract the Table name, column name, constraint name etc., from the string and query against the Data dictionaries - USER_TABLES, USER_TAB_COLUMNS, USER_CONSTRAINTS etc.
3. If it matches with the Data dictionary tables then say "Verification Success" else show the difference.

for example:
following are the DDL statement submitted to DBA - (and DBA has already implemented the changes to DB tables)
these are the input string to the script:
Code:
ALTER TABLE supplier ADD supplier_name  varchar2(50);

ALTER TABLE suppliers RENAME TO vendors;

ALTER TABLE supplier ADD (supplier_name    varchar2(50),city    varchar2(45) );
     
ALTER TABLE supplier MODIFY supplier_name   varchar2(100) not null;
 
ALTER TABLE supplier MODIFY (supplier_name    varchar2(100)    not null, city    varchar2(75));

ALTER TABLE supplier DROP COLUMN supplier_name;
 
ALTER TABLE supplier RENAME COLUMN supplier_name to sname;

Script should parse thr these statement word by word, if first 2 words is ALTER TABLE, pick the table name into a variable(v_tab_name).
if the 4th letter is ADD - pick the 5th word into a variable(v_col_name)
connect to Database and do the following query:

Code:
select 1 from USER_TABLES
where table_name = v_tab_name
and column_name = v_col_name

If this query returns one row, then print verification success

Hope this gives a better pic of what I am trying to do.

Last edited by Scott; 08-29-2010 at 06:20 AM.. Reason: Please use code tags
# 4  
Old 08-28-2010
This should give you enough of an idea of how to write the script. This example assumes Ksh (Korn shell) as bash has issues with piping command output to read into variable names.

Code:
#!/usr/bin/env ksh
while [[ -n $1 ]]       # for each string on the command line
do
        echo "$1" | read cmd1 cmd2 table_name sub_cmd column_name junk
        if [[ $1" "$2 == "ALTER TABLE" ]]
        then
                if [[ $sub_cmd == "ADD" ]]
                then
                        database_command "select 1 from USER_TABLES where table_name = $table_name and column_name = ${column_name/\(/}" | wc -l | read count
                        # assuming that one record out from database command is a good response and any other amount of output is not
                        if (( $count != 1 ))
                        then
                                echo "error verifying $1"
                        else
                                echo "verified: $1"
                        fi
                fi
        fi

        shift
done

I have no way to actually test this, but it does pass syntax checking by the shell.

Have fun.
This User Gave Thanks to agama For This Post:
# 5  
Old 08-28-2010
Thanks Agama, I can build other conditions using this template.
Could you please share the database_connect function too?
# 6  
Old 08-28-2010
Unfortunately I have no data base magic in my head. I don't work with them and that is why I used "database_command" in place of some real db interface. I'm sure there is some 'send-sql' or similar command line interface depending on the database you are using, but I have no experience to even start to point you in the right direction.
# 7  
Old 08-28-2010
I was thinking if AWK can do this. Please suggest.

Code:
ALTER TABLE <   > ADD <   > <   >;

Can I search for this pattern and extract content between the < > into a variable, like:

Code:
var1=tab_nm
var2=col_nm
var3=col_type


Last edited by Scott; 08-29-2010 at 06:21 AM.. Reason: Updated lines from other post
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Dummies Questions & Answers

How to pass cat file in awk statement?

Hi, I am working on kernel parameters, want to check values under /proc/sys/kernel below I tried for kernel.sem SEMMNS: 4096 cat /proc/sys/kernel/sem | awk '{print $2}' awk '{ if ($2 < 33000) print }' /proc/sys/kernel/sem |awk '{print $2}' 32000 The above... (7 Replies)
Discussion started by: stew
7 Replies

3. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

4. Shell Programming and Scripting

How to pass tablenames from a file to shell script to execute create statement in DB2

Hi, I am new to Shell Scripting, and I need to create nicknames for 600 tables in db2. I have the file names in a text file and i have to pass these table names to a shell script create nicknames in db2. Can some one please help me in this regard. (1 Reply)
Discussion started by: kamalanaatha
1 Replies

5. Shell Programming and Scripting

How to use loop to convert a DML statement into DDL?

Hi Unix Gurus, I am a newb. I am creating a script which will use an input file. This input file can have 1 or more than 1 DML staments like INSERT/DELETE/UPDATE. I have to execute these statements using my script but before execution of these DML statements, I need to check the count for... (17 Replies)
Discussion started by: ustechie
17 Replies

6. Shell Programming and Scripting

How to call an sql script inside a while statement in KSH

Hi all, I'm trying to run an sql inside a loop which looks like this #!bin/ksh while IFS=, read var1 var2 do sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF insert into ${TABLE} ( appt_date ) values ( '${var1 }' ); ... (6 Replies)
Discussion started by: ryukishin_17
6 Replies

7. Shell Programming and Scripting

how to use if statement in ksh script

Hi, I need to compare two variables using if condition and i am not sure if am right or wrong. My code is like : if then echo "new file" else echo "old file and remove it" fi where both variables contain time : filetime contains the time when a file... (2 Replies)
Discussion started by: manmeet
2 Replies

8. Shell Programming and Scripting

I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to. The goal: -to... (6 Replies)
Discussion started by: DeCoTwc
6 Replies

9. Shell Programming and Scripting

Help with if statement in ksh script

I need a way to grep for a string in a file and if it finds it, to print set a variable to "Yes", if it doesn't find the string in a file to set the variable to "No". I plan on using these variables to print a table that lists whether the string was found or not. For example print "File ... (2 Replies)
Discussion started by: stepnkev
2 Replies

10. Shell Programming and Scripting

How to pass variable to SQLPLUS in a ksh script?

Hi, I am writing a ksh script which will use sqlplus to run a sql and pass 2 variables as the SQL request. In the ksh script, I have 2 variables which are $min_snap and $max_snap holding 2 different numbers. Inside the same script, I am using SQLPLUS to run an Oracle SQL script,... (6 Replies)
Discussion started by: rwunwla
6 Replies
Login or Register to Ask a Question