Example of
Calling the Loader with Perl Script
If a command is changed at runtime, for example, because user inputs are part of the command, syntax errors can easily occur.
The SQL method only provides SQL error codes. You need exceptions to intercept syntax errors in the
Create a database session
Log on to the database instance
Use a Loader command to load data into the table and intercept exceptions
Log off
An incorrect data path in the following example can lead to a syntax error or data access error.
# Reference to the SAP DB Perl Library
# ------------------------------------
use SAP::DBTECH::loader;
# Parse the call arguments
# --------------------------
$userid = $ARGV[0];
$password = $ARGV[1];
$database_name = $ARGV[2];
$data_path = $ARGV[3];
$server_node = "localhost";
# Create a database session for the Loader
# -------------------------------------------
$session = loader::Loader ($server_node, $database_name);
# Log on to the database instance
# --------------------------------
$session->cmd("use user $userid $password;");
# Example of Exception Handling
# -----------------------------
eval{
$loadrc=$session->sql ("DATALOAD TABLE customer ".
"cno 1-4".
"name 6-12".
"zip 14-18".
"city 20-31".
"INFILE $data_path\customer.dat" );
print "$loadrc\n";}
if ($@){
print "command failed: $@\n";
}
# End the database session
# ----------------------------
undef $session