UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old August 19th, 2004, 06:28 PM
svvc svvc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 12 svvc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 58 sec
Reputation Power: 0
truncate table script

Hi,
I'm new to unix.
i know oracle. Can somebody help me in writing a unix script to truncate a table when we pass that table name as command line argument.
Thanks in advance
Sachin

Reply With Quote
  #2  
Old August 19th, 2004, 10:50 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,077 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 13 h 40 m 11 sec
Reputation Power: 8
man tail head split sed ...

Reply With Quote
  #3  
Old August 20th, 2004, 04:27 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
It's just a little more than reading man pages....

Code:
#!/bin/ksh
# $1 = table name
# UID= user  PSWD=password
exsql ()  {
 awk '{ if(NR == 1) {
        STR=sprintf("%s/%s",UI,PW)
        print STR
        print "set pagesize 0;"
        print "set tab off;"      
        print "set arraysize 1;"
        }
        print $0}' UI=$UID PW=$PSWD | $ORACLE_HOME/bin/sqlplus -s 
 }

echo "TRUNCATE TABLE $1;"|exsql > truncate_table.log
exit

Reply With Quote
  #4  
Old September 9th, 2004, 10:27 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Hi Sachin,

I find it simplest to use a "here document" within a UNIX script to execute SQL commands.

I've attached an example below that is using external o/s authentication (the whole passing usernames and passwords to Oracle via a script is a bit messy if you don't want the password hardcoded in the script, or showing up on the ps listing).

Note that the $ is a special character in the UNIX shell so if you use it in a here document you need to escape it with a "\" (see how I've had to run "select * from v$instance;" below).

Good luck,
Andy


#!/usr/bin/ksh

#-- Get the table to be truncated
TABLE_NAME=$1

#-- Exit if the table_name has not been supplied
if [ -z "${TABLE_NAME}" ]; then
echo "ERROR: table_name not supplied"
exit 10
fi

#-- Execute the SQL
sqlplus / <<+ENDOFSQL+
select * from v\$instance;
truncate table ${TABLE_NAME};
exit
+ENDOFSQL+

exit 0

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > truncate table script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway