Skip to content

Instantly share code, notes, and snippets.

@earnhardt3rd
Created June 19, 2023 13:56
Show Gist options
  • Select an option

  • Save earnhardt3rd/bb6d8a9bfee175ac916d8791f1fa678c to your computer and use it in GitHub Desktop.

Select an option

Save earnhardt3rd/bb6d8a9bfee175ac916d8791f1fa678c to your computer and use it in GitHub Desktop.
require 'getopts.pl';
Getopts('m:d:t');
my $MODE = uc($opt_m) || "";
my $DEBUG = $opt_d || 0;
if (defined $opt_t) { my %TEST = &execSQL();}
1;
sub execSQL {
my $SQL = shift || "";
my $DBMS_HOST = shift || $_DBMS{"HOST"} . "\." . $_DBMS{"DOMAIN"};
my $DBMS_USER = shift || $_DBMS{"USER"};
my $DBMS_PASS = shift || $_DBMS{"PASS"};
my $DBMS_DBMS = shift || $_DBMS{"MDB"};
if ($DBMS_HOST eq "") {print " ERROR! Can not continue without DBMS_HOST\n";return;}
if ($DBMS_USER eq "") {print " ERROR! Can not continue without DBMS_USER\n";return;}
if ($DBMS_PASS eq "") {print " ERROR! Can not continue without DBMS_PASS\n";return;}
if ($SQL eq "") {print " ERROR! Can not continue without SQL\n";return;}
my $sqlCmd="sqlcmd -S${DBMS_HOST} -U${DBMS_USER} -s, -W";
if ($DBMS_MDB ne ""){$sqlCmd .= " -d${DBMS_MDB}";}
$sqlCmd .= " -Q\"${SQL}\"";
print "--SQL\n {\n" if $DEBUG > 0;
print " $sqlCmd\n" if $DEBUG > 0;
$sqlCmd .= " -P${DBMS_PASS}";
my $sqlCmdX="";
if (uc($SQL) =~ /^SELECT/) {
$sqlCmdX = `$sqlCmd`;
} else {
print " ** SHOW ONLY **\n" if $SHOW >=0;
if ($SHOW <0) {
$sqlCmdX = `$sqlCmd`;
print " $sqlCmdX\n";
}
}
my @SQL_RTN = split('\n',$sqlCmdX);
my @RTN;
if ($DEBUG > 0) {
for (my $si=2;$si<=scalar(@SQL_RTN)-1;$si++) {
next if ($SQL_RTN[$si] =~ /rows affected/);
next if ($SQL_RTN[$si] eq "");
print " $SQL_RTN[$si]\n";
push(@RTN,$SQL_RTN[$si]);
}
print " }\n";
}
return @RTN;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment