| Index Search Add FAQ Ask Question |
|---|
$Date: 08-Apr-2004 $
$Revision: 1.01 $
$Author: Frank Naudé $
What is TCL and OraTCL and what's it got to do with Oracle?
TCL stands for "Tool Command Language" and is pronounced "tickle."
TCL is a portable scripting language similar to Perl
and PHP that can run on different platforms
like Unix, Linux, Windows, MAC, etc.
OraTCL is an Oracle OCI-like extension to the TCL Language. OraTCL allows one to run SQL and PL/SQL statements from TCL programs and get the results back in strings.
OraTCL was adopted by Oracle Corporation and is used for several of the Enterprise Manager (OEM) Event and Job scripts. OraTCL is installed with the OEM Inteligent Agent, and is thus always available for use by Oracle DBAs and developers.
TCL scripts are executed with the "tclsh" command interpreter. Look at this example TCL Script:
puts stdout "Please enter your name:" gets stdin name puts stdout "Hello $name"
Interactive Mode:
$ oratclsh ORATCLSH for Solaris: Version 9.2.9.4.0 - Production on 08-OCT-2003 21:14:33 (c) Copyright 2003 Oracle Corporation. All rights reserved. oratclsh[1]- set x "Look Ma, " oratclsh[2]- set y "I can write TCL scripts." oratclsh[3]- puts "$x $y" Look Ma, I can write TCL script. oratclsh[4]- exit
Batch Mode: Write a TCL script, say, hello.tcl and execute with command "oratclsh hello.tcl":
puts "Hello World...\n" puts "TCL version = [info tclversion]" puts "Script Name = [info script]" exit 0
set connect scott/tiger@orcl
set lda [oralogon $connect]
if {$oramsg(rc) == 0} { # ????
puts "Successfully connected to Oracle."
} else {
puts "Unable to connect: $oramsg(rc)"
}
oralogoff $lda
set connect scott/tiger@orcl
set lda [oralogon $connect]
set cur [oraopen $lda]
set sql {select tname from tab}
orasql $cur $sql
set row [orafetch $cur]
while {$oramsg(rc) == 0} {
puts $row
set row [orafetch $cur]
}
oraclose $cur
oralogoff $lda
OraTCL does not autocommit transactions by default. Nevertheless, one can enable or disable AUTOCOMMIT using the following syntax:
oraautocom logon-handle true oraautocom logon-handle false
oralogon scott/tiger
if {$oramsg(rc) != ""} {
puts stderr "Unable to connect to database:"
puts stderr " Error Code = ORA-$oramsg(rc)"
puts stderr " Error Message = $oramsg(errortxt"
exit
}
package require Oratcl
set con [oralogon "URANO/AHONDARA@ZMDB"]
set plblock {
begin
SELECT count(*) into :n
FROM XQTE.FAC_PARTS_MASTER
WHERE maker_code = :mcode;
end;
}
set cur [oraopen $con]
foreach e {"SELF" "BMON12"} {
oraplexec $cur $plblock :n "" :mcode $e
set r [orafetch $cur]
puts "maker=[lindex $r 1] count=[lindex $r 0]"
}
oraclose $cur
oralogoff $con
![]() |
![]() |