on 02-06-2009 4:47 AM
Hi everyone,
Is that possible to use Perl script to access and extract SAP table ? Is there any documentation for this ?
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
$ENDTIME = substr($ROW, 157, 6);
$EHOUR = trim(substr($ENDTIME, 0, 2));
if ($EHOUR == 24) { $EHOUR = "00"; }
$EMINUTE = trim(substr($ENDTIME, 2, 2));
$ESECOND = trim(substr($ENDTIME, 4, 2));
$STATUS = substr($ROW, 163, 1);
if ($SCHTIME && $STRTIME && $SYEAR && $BYEAR) {
($LY,$LM,$LD, $Lh,$Lm,$Ls) = Delta_YMDHMS($SYEAR,$SMONTH,$SDAY, $SHOUR,$SMINUTE,$SSECOND, $BYEAR,$BMONTH,$BDAY, $BHOUR,$BMINUTE,$BSECOND);
$DELAY = ($Ls + ($Lm * 60) + ($Lh * 3600) + ($LD * 86400)); $LY = ""; $LM = "";
}
if ($ENDTIME && ($ENDDATE >= $STRDATE) && $BYEAR && $EYEAR) {
($DY,$DM,$DD, $Dh,$Dm,$Ds) = Delta_YMDHMS($BYEAR,$BMONTH,$BDAY, $BHOUR,$BMINUTE,$BSECOND, $EYEAR,$EMONTH,$EDAY, $EHOUR,$EMINUTE,$ESECOND);
$DURATION = ($Ds + ($Dm * 60) + ($Dh * 3600) + ($DD * 86400));
if ($DURATION < 0) {
$DURATION = 0;
print "Duration less than zero, set to zero\n";
}
$DY = ""; $DM = "";
} else {
$DURATION = 0;
}
print OUTFILE ("$SAPSID,$JOBNAME,$JOBID,$JOBCLASS,$PERIODIC,$SERVER,$CREATOR,$USERNAME,$MANDT,$EVENT,$SCHDATE,$SCHTIME,$STRDATE,$STRTIME,$ENDDATE,$ENDTIME,$DELAY,$DURATION,$STATUS\n"); $JOBCOUNT ++;
}
$RFC->close();
close OUTFILE;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've apparently put too much code in above, so I'm splitting it into two posts to make it more readable:
#!/usr/bin/perl -w
use SAP::Rfc;
### Variable Declarations
$DEBUG = 0;
$OUTPUT = "sm37.csv";
$JOBCOUNT = 0;
@ICONFIG = ();
$CFGCTR = 0;
### End of variable declarations
open(OUTFILE, ">>$OUTPUT") || die ("cannot open output file ", $OUTPUT, "\n");
my $RFC = new SAP::Rfc(
ASHOST => 'servername',
USER => 'username',
PASSWD => 'password',
LANG => 'EN',
CLIENT => '010',
SYSNR => '01',
TRACE => '0' );
my $IT = $RFC->discover("RFC_READ_TABLE");
$IT->QUERY_TABLE('TBTCO');
$IT->OPTIONS( ["ENDDATE = $MAXDATE AND ENDTIME > $MAXTIME OR ENDDATE > $MAXDATE"] );
my @FLDARRAY = qw(JOBNAME JOBCOUNT JOBCLASS PERIODIC REAXSERVER RELUNAME SDLUNAME AUTHCKMAN EVENTID SDLSTRTDT SDLSTRTTM STRTDATE STRTTIME ENDDATE ENDTIME STATUS);
$IT->FIELDS([@FLDARRAY]);
$RFC->callrfc( $IT );
print "Num Rows in PRD matching selection criteria: ".$IT->tab('DATA')->rowCount()." \n";
for my $ROW ( $IT->DATA ) {
print "\n\nRaw row: $ROW";
$JOBNAME = trim(substr($ROW, 0, 32));
$JOBID = substr($ROW, 32, 8);
$JOBCLASS = substr($ROW, 40, 1);
if (substr($ROW, 41, 1)) {
$PERIODIC = 1;
} else {
$PERIODIC = 0;
}
$SERVER = trim(substr($ROW, 42, 20));
$CREATOR = trim(substr($ROW, 62, 12));
$USERNAME = trim(substr($ROW, 74, 12));
$MANDT = substr($ROW, 86, 3);
$EVENT = trim(substr($ROW, 89, 32));
$SCHDATE = substr($ROW, 121, 8);
$SYEAR = trim(substr($SCHDATE, 0, 4));
$SMONTH = trim(substr($SCHDATE, 4, 2));
$SDAY = trim(substr($SCHDATE, 6, 2));
$SCHTIME = substr($ROW, 129, 6);
$SHOUR = trim(substr($SCHTIME, 0, 2));
if ($SHOUR == 24) { $SHOUR = "00"; }
$SMINUTE = trim(substr($SCHTIME, 2, 2));
$SSECOND = trim(substr($SCHTIME, 4, 2));
$STRDATE = substr($ROW, 135, 8);
$BYEAR = trim(substr($STRDATE, 0, 4));
$BMONTH = trim(substr($STRDATE, 4, 2));
$BDAY = trim(substr($STRDATE, 6, 2));
$STRTIME = substr($ROW, 143, 6);
$BHOUR = trim(substr($STRTIME, 0, 2));
if ($BHOUR == 24) { $BHOUR = "00"; }
$BMINUTE = trim(substr($STRTIME, 2, 2));
$BSECOND = trim(substr($STRTIME, 4, 2));
$ENDDATE = substr($ROW, 149, 8);
$EYEAR = trim(substr($ENDDATE, 0, 4));
$EMONTH = trim(substr($ENDDATE, 4, 2));
$EDAY = trim(substr($ENDDATE, 6, 2));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here's a bit of script using SAP::Rfc to read the table TBTCO, which stores background job information. You can find the whole script here: http://www.sapdba.com/forums/viewtopic.php?f=3&t=6. I've removed most of it so that the following is just an example of reading the table and printing the results to stdout:
(note, this was written on a 4.6B system)
#!/usr/bin/perl -w
use SAP::Rfc;
### Variable Declarations
$DEBUG = 0;
$OUTPUT = "sm37.csv";
$JOBCOUNT = 0;
@ICONFIG = ();
$CFGCTR = 0;
### End of variable declarations
open(OUTFILE, ">>$OUTPUT") || die ("cannot open output file ", $OUTPUT, "\n");
my $RFC = new SAP::Rfc(
ASHOST => 'servername',
USER => 'username',
PASSWD => 'password',
LANG => 'EN',
CLIENT => '010',
SYSNR => '01',
TRACE => '0' );
my $IT = $RFC->discover("RFC_READ_TABLE");
$IT->QUERY_TABLE('TBTCO');
$IT->OPTIONS( ["ENDDATE = $MAXDATE AND ENDTIME > $MAXTIME OR ENDDATE > $MAXDATE"] );
my @FLDARRAY = qw(JOBNAME JOBCOUNT JOBCLASS PERIODIC REAXSERVER RELUNAME SDLUNAME AUTHCKMAN EVENTID SDLSTRTDT SDLSTRTTM STRTDATE STRTTIME ENDDATE ENDTIME STATUS);
$IT->FIELDS([@FLDARRAY]);
$RFC->callrfc( $IT );
print "Num Rows in PRD matching selection criteria: ".$IT->tab('DATA')->rowCount()." \n";
for my $ROW ( $IT->DATA ) {
print "\n\nRaw row: $ROW";
$JOBNAME = trim(substr($ROW, 0, 32));
$JOBID = substr($ROW, 32, 8);
$JOBCLASS = substr($ROW, 40, 1);
if (substr($ROW, 41, 1)) {
$PERIODIC = 1;
} else {
$PERIODIC = 0;
}
$SERVER = trim(substr($ROW, 42, 20));
$CREATOR = trim(substr($ROW, 62, 12));
$USERNAME = trim(substr($ROW, 74, 12));
$MANDT = substr($ROW, 86, 3);
$EVENT = trim(substr($ROW, 89, 32));
$SCHDATE = substr($ROW, 121, 8);
$SYEAR = trim(substr($SCHDATE, 0, 4));
$SMONTH = trim(substr($SCHDATE, 4, 2));
$SDAY = trim(substr($SCHDATE, 6, 2));
$SCHTIME = substr($ROW, 129, 6);
$SHOUR = trim(substr($SCHTIME, 0, 2));
if ($SHOUR == 24) {
$SHOUR = "00";
}
$SMINUTE = trim(substr($SCHTIME, 2, 2));
$SSECOND = trim(substr($SCHTIME, 4, 2));
$STRDATE = substr($ROW, 135, 8);
$BYEAR = trim(substr($STRDATE, 0, 4));
$BMONTH = trim(substr($STRDATE, 4, 2));
$BDAY = trim(substr($STRDATE, 6, 2));
$STRTIME = substr($ROW, 143, 6);
$BHOUR = trim(substr($STRTIME, 0, 2));
if ($BHOUR == 24) {
$BHOUR = "00";
}
$BMINUTE = trim(substr($STRTIME, 2, 2));
$BSECOND = trim(substr($STRTIME, 4, 2));
$ENDDATE = substr($ROW, 149, 8);
$EYEAR = trim(substr($ENDDATE, 0, 4));
$EMONTH = trim(substr($ENDDATE, 4, 2));
$EDAY = trim(substr($ENDDATE, 6, 2));
$ENDTIME = substr($ROW, 157, 6);
$EHOUR = trim(substr($ENDTIME, 0, 2));
if ($EHOUR == 24) {
$EHOUR = "00";
}
$EMINUTE = trim(substr($ENDTIME, 2, 2));
$ESECOND = trim(substr($ENDTIME, 4, 2));
$STATUS = substr($ROW, 163, 1);
if ($SCHTIME && $STRTIME && $SYEAR && $BYEAR) {
($LY,$LM,$LD, $Lh,$Lm,$Ls) = Delta_YMDHMS($SYEAR,$SMONTH,$SDAY, $SHOUR,$SMINUTE,$SSECOND,
$BYEAR,$BMONTH,$BDAY, $BHOUR,$BMINUTE,$BSECOND);
$DELAY = ($Ls + ($Lm * 60) + ($Lh * 3600) + ($LD * 86400));
$LY = ""; $LM = "";
}
if ($ENDTIME && ($ENDDATE >= $STRDATE) && $BYEAR && $EYEAR) {
($DY,$DM,$DD, $Dh,$Dm,$Ds) = Delta_YMDHMS($BYEAR,$BMONTH,$BDAY, $BHOUR,$BMINUTE,$BSECOND,
$EYEAR,$EMONTH,$EDAY, $EHOUR,$EMINUTE,$ESECOND);
$DURATION = ($Ds + ($Dm * 60) + ($Dh * 3600) + ($DD * 86400));
if ($DURATION < 0) {
$DURATION = 0;
print "Duration less than zero, set to zero\n";
}
$DY = ""; $DM = "";
} else {
$DURATION = 0;
}
print OUTFILE ("$SAPSID,$JOBNAME,$JOBID,$JOBCLASS,$PERIODIC,$SERVER,$CREATOR,$USERNAME,$MANDT,$EVENT,$SCHDATE,$SCHTIME,$STRDATE,$STRTIME,$ENDDATE,$ENDTIME,$DELAY,$DURATION,$STATUS\n");
$JOBCOUNT ++;
}
$RFC->close();
close OUTFILE;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.