| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | require 5.004; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use DBIx::XML_RDB; | ||
| 8 | use Getopt::Long; | ||
| 9 | use vars qw($datasource $driver $userid $password $table $outputfile $help $dbname $verbose @fields); | ||
| 10 | |||
| 11 | sub usage; | ||
| 12 | |||
| 13 | # Options to variables mapping | ||
| 14 | my %optctl = ( | ||
| 15 | 'sn' => \$datasource, | ||
| 16 | 'uid' => \$userid, | ||
| 17 | 'pwd' => \$password, | ||
| 18 | 'table' => \$table, | ||
| 19 | 'output' => \$outputfile, | ||
| 20 | 'help' => \$help, | ||
| 21 | 'db' => \$dbname, | ||
| 22 | 'driver' => \$driver, | ||
| 23 | 'verbose' => \$verbose ); | ||
| 24 | |||
| 25 | # Option types | ||
| 26 | my @options = ( | ||
| 27 | "sn=s", | ||
| 28 | "uid=s", | ||
| 29 | "pwd=s", | ||
| 30 | "table=s", | ||
| 31 | "output=s", | ||
| 32 | "db=s", | ||
| 33 | "driver=s", | ||
| 34 | "help", | ||
| 35 | "verbose" | ||
| 36 | ); | ||
| 37 | |||
| 38 | GetOptions(\%optctl, @options) || die "Get Options Failed"; | ||
| 39 | |||
| 40 | usage if $help; | ||
| 41 | |||
| 42 | unless ($datasource && $userid && $table && $outputfile) { | ||
| 43 | usage; | ||
| 44 | } | ||
| 45 | |||
| 46 | $driver = $driver || "ODBC"; # ODBC is the default. Change this if you wish. | ||
| 47 | |||
| 48 | my $xmlout = DBIx::XML_RDB->new($datasource, $driver, $userid, $password, $dbname) | ||
| 49 | || die "Failed to make new xmlout"; | ||
| 50 | |||
| 51 | $xmlout->DoSql("SELECT * FROM $table ORDER BY 1"); | ||
| 52 | |||
| 53 | use IO::File; | ||
| 54 | |||
| 55 | my $output = IO::File->new(">". $outputfile); | ||
| 56 | |||
| 57 | print $output $xmlout->GetData; | ||
| 58 | |||
| 59 | # End | ||
| 60 | |||
| 61 | sub usage { | ||
| 62 | print <<EOF; | ||
| 63 | Usage: | ||
| 64 | sql2xls.pl {Options} | ||
| 65 | |||
| 66 | where options are: | ||
| 67 | |||
| 68 | Option ParamName ParamDesc | ||
| 69 | -sn servername Data source name | ||
| 70 | [-driver dbi_driver] Driver that DBI uses. Default is ODBC | ||
| 71 | -uid username Username | ||
| 72 | [-pwd password] Password | ||
| 73 | -table tablename Table to extract | ||
| 74 | -output outputfile File to place output in (excel file) | ||
| 75 | [-db dbname] Sybase database name | ||
| 76 | [-v or --verbose] Verbose output | ||
| 77 | EOF | ||
| 78 | exit; | ||
| 79 | } |