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
# 8  
Old 08-28-2010
Hi
Code:
# cat a
ALTER TABLE abc ADD empid char
# awk  '{if($1=="ALTER" && $4="ADD"){print "var"++i"="$3"\n" "var"++i"="$5"\n" "var"++i"="$6}}' a
var1=abc
var2=empid
var3=char
#

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 9  
Old 08-29-2010
Thanks Guru,
just another change needed.

var1, var2, var3 are the variables i need to set from within the awk and use them outside the awk, How can I do that?
# 10  
Old 08-29-2010
Most efficient if you use Ksh:

Code:
awk  '{if( $1=="ALTER" && $4="ADD" )  print $3, $5, $6 }' file | read table column type

Bash dosen't support piping int read like this so the only way I know is to direct it to a temp file and read from the tmp file, which results in additional overhead that adds up significantly if you must execute this a lot.

If you must use bash:
Code:
awk  '{ if( $1=="ALTER" && $4="ADD" )  print $3, $5, $6 }' file >/tmp/x.$$
read table column type </tmp/.x$$ 
rm /tmp/x.$$

This User Gave Thanks to agama For This Post:
# 11  
Old 08-29-2010
Code:
var1=$(awk -F "[<>]" '{print $2}' file);var2=$(awk -F "[<>]" '{print $4}' file);var3=$(awk -F "[<>]" '{print $6}' file)

This User Gave Thanks to protocomm For This Post:
# 12  
Old 08-29-2010
Quote:
Originally Posted by agama
Bash dosen't support piping int read like this so the only way I know is to direct it to a temp file and read from the tmp file, which results in additional overhead that adds up significantly if you must execute this a lot.
Bash does support piping into read but the read is in a subshell, so that when the read ends the variables are lost... You can do something like this:

Code:
awk  '$1=="ALTER" && $4="ADD" { print $3, $5, $6 }' infile |  
{ 
read table column type 
echo $table
echo $column
echo etcetera...
}

Alternatively, bash supports this construct:
Code:
read table column type < <( awk  '$1=="ALTER" && $4="ADD" { print $3, $5, $6 }' infile )

This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 08-29-2010
Appreciate the responses, as I am new to KSH .. its helping me learn a lot.
So here is what I modified it.
Code:
#!/bin/ksh

while read line
do
	echo $line | awk  '{
		if($1=="ALTER" && $2=="TABLE"){
			if($4=="ADD"){
				sql_statement=sprintf("select 1 from USER_TABLES where table_name = %s and column_name = %s and column_type = %s", $3, $5, $6);
				print sql_statement
			}
			if($4=="MODIFY"){
				sql_statement=sprintf("select 1 from USER_TABLES where table_name = %s and column_name = %s and column_type = %s", $3, $5, $6);
				print sql_statement
			}
			if($4=="RENAME" && $5=="TO" ){
				sql_statement=sprintf("select 1 from USER_TABLES where table_name = %s", $6);
				print sql_statement
			}
			if($4=="RENAME" && $5=="COLUMN"){
				sql_statement=sprintf("select 1 from USER_TABLES where table_name = %s and column_name = %s", $3, $8);
				print sql_statement
			}
                        if($4=="DROP" && $5=="COLUMN"){
				sql_statement=sprintf("select 1 from USER_TABLES where table_name = %s and column_name = %s", $3, $6);
				print sql_statement
			}
		}
	}' | read sql
	echo $sql
done < file

Input File: file
Code:
1. ALTER TABLE emp ADD status VARCHAR2(20);
2. ALTER TABLE dept MODIFY dept_name varchar2(20) NOT NULL;
3. ALTER TABLE temp RENAME TO temp01;
4. ALTER TABLE dept RENAME COLUMN status TO status_new;
5. ALTER TABLE emp DROP COLUMN status;

6. ALTER TABLE customers ADD (contact_name	varchar2(50),last_contacted date );
7. ALTER TABLE customers MODIFY ( customer_name varchar2(50) not null, state varchar2(2));

With all of yours help, I was able to handle first 5 conditions. What is the best way to handle 6th and 7th?
BTW I am using KSH shell scripting.

Last edited by Scott; 08-29-2010 at 04:52 PM.. Reason: Code tags, please...
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