Index   Search   Add FAQ   Ask Question  
 

Please note: This page is not maintained anymore. Please visit the new and improved FAQ at http://www.orafaq.com/faq/tcl

Oracle/ TCL FAQ

$Date: 08-Apr-2004 $
$Revision: 1.01 $
$Author: Frank Naudé $

Topics

  • What is TCL and OraTCL and what's it got to do with Oracle?
  • How does one start and interact with OraTCL?
  • How does one connect to Oracle?
  • How does one SELECT, INSERT, UPDATE and DELETE data from OraTCL?
  • How are database transactions handled in OraTCL?
  • How are database errors handled in OraTCL?
  • How does one call stored procedures from OraTCL?
  • Where can one find more info about TCL and Oracle?

  • Back to Oracle FAQ Index

    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"
    
  • Back to top of file

  • How does one start and interact with OraTCL?

    One can start OraTCL in interactive or batch mode by running the "oratclsh" command (Unix and Windows) located in your ORACLE_HOME/bin directory.

    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
    

  • Back to top of file

  • How does one connect to Oracle?

    One can use the "oralogon" function to establish an Oracle connection and the "oralogoff" function to disconnect from the database. Look at this sample OraTCL script:
    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
    

  • Back to top of file

  • How does one SELECT, INSERT, UPDATE and DELETE data from OraTCL?

    Look at this example:
    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
    

  • Back to top of file

  • How are database transactions handled in OraTCL?

    The "ORACOMMIT logon-handle" and "ORAROLL logon-handle" function calls can be used to commit or rollback pending transactions.

    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
    

  • Back to top of file

  • How are database errors handled in TCL?

    After each OCI command the "rc" variable are set to either 0 for successfull execution, or the Oracle server error. OraTcl maintains a Tcl global array called "oramsg" that provides information on Oracle server error messages.
    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
    }
    

  • Back to top of file

  • How does one call stored procedures from OraTCL?

    One can execute procedures, functions and anonymous PL/SQL blocks from OraTCL using the "oraplexec" function call. Look at this example:
    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
    

  • Back to top of file

  • Where can one find more info about TCL and Oracle?

  • Back to top of file
  • HOME | ASK QUESTION | ADD FAQ | SEARCH | E-MAIL US