AllExperts > Oracle 
Search      
Oracle
Volunteer
Answers to thousands of questions
 Home · More Oracle Questions · Answer Library  · Encyclopedia ·
More Oracle Answers
Question Library

Ask a question about Oracle
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Suchitra Joshi
Expertise
I can answer questions regarding SQL, PL/SQL, Procedures, Functions, Triggers, SQL Loader, Oracle Forms, Oracle Reports, and some basic dba and performance tuning activities.

Experience
15+ years of Oracle PL/SQL Development

Education/Credentials
B.Sc (Electronics), Diploma in Computer Applications (DCA)

Awards and Honors
OCP - SQL and PL/SQL
Brainbench certifications in Oracle Administration, PL/SQL, Developer 2000

 
   

You are here:  Experts > Computing/Technology > Oracle > Oracle > Creating multiple tables with Shell Script

Oracle - Creating multiple tables with Shell Script


Expert: Suchitra Joshi - 10/9/2009

Question
Hello Suchi,

I'm looking for a pure sql Cum Shell solution to the problem described below. I'm a beginner in shell and SQL scripting and have no knowledge of PL/SQL. Therefore i'm trying to limit my focus to SQL and Shell Scripting.

I'm using an Oracle 10g database and a Sun Solaris machine

I've figured out the following process to create multiple tables using a shell script :

[code]
#! /bin/bash

for i in *
          do
sqlplus username/password@db_name<<EOF
   create table1 test select account_no, balance from records_all;
EOF   -- create the table and insert records

OUTPUT=$(sqlplus -silent username/password@db_name << DONE
set pages 0 feedback off
select count(1) from test1;
DONE)
echo " Number of records in the table is:$OUTPUT "  -- count the number of records in the created table


if [$OUTPUT -eq 2077613 ]; then   -- check if table creation is successful
   echo "Table creation is successful"
   echo "Table has successfully stored $OUTPUT records"
   exit 0
else
   echo "Un successful in creating the table"
fi
done

[code]


My question is :

How do i go about changing the name of the table, each time the prompt enter's the for loop ? i.e., The first time it creates  "table1". I want to create table2, table3 and so on upto table10.

Appreciate if you can guide me with the syntax.

Regards

kris  

Answer
Hi Kris,

Try the following -

[code]
#! /bin/bash

tname="table"

for i in *
do
sqlstr="create table $tname$i as select account_no, balance from records_all"
sqlplus username/password@db_name<<EOF
  execute immediate $sqlstr;
EOF   -- create the table and insert records

OUTPUT=$(sqlplus -silent username/password@db_name << DONE
set pages 0 feedback off
select count(1) from $tname$i;
DONE)
echo " Number of records in the table is:$OUTPUT "  -- count the number of records in the created table


if [$OUTPUT -eq 2077613 ]; then   -- check if table creation is successful
  echo "Table creation is successful"
  echo "Table has successfully stored $OUTPUT records"
  exit 0
else
  echo "Un successful in creating the table"
fi
done

[code]


I hope this solution works.

Regards

Suchitra

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.