Trying to work with a database on remote machines can be a pain sometimes...
This script will dump SP_HELP to the web browser, and allow the user to select one or more tables. Selected tables will then have their structure shown.
use strict; use DBI; use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); $|++; print header (); print start_html; my $db_username="user"; my $db_password="password"; my $db_serverstring="DBI:Driver:here"; my $dbh = DBI->connect ($db_serverstring, $db_username, $db_password) +|| die ("Cannot connect to database -- DBI reports ", $DBI::errstr); print "\n"; if (param("view")) { h3"<CENTER>Viewing tables</CENTER>"; my @tables = param("view"); foreach (@tables) { my $sql = $dbh->prepare ("sp_columns $_") || die;; my $sth = $sql->execute or die "no ".$DBI::errstr; print h3("<CENTER>Viewing table $_</CENTER>"),"<TABLE Border = + 1>\n"; my $THdone; while (my $result_ref = $sql->fetchrow_hashref()){ unless ($THdone) { foreach (sort keys %$result_ref) { print "<TH>$_ </TH>"; } $THdone = "yes, indeedy!"; } print "<TR>"; foreach (sort keys %$result_ref) { if (! defined $$result_ref{$_}) {print "<TD><I>undef</ +i></TD>";next} print "<TD>$$result_ref{$_}</TD>"; } print "</TR>\n"; } print "</TABLE><BR><BR>"; } } else { print h2"<CENTER>Viewing sp_help</CENTER>"; print start_form; my $sql = $dbh->prepare ("sp_help") || die;; my $sth = $sql->execute or die "no ".$DBI::errstr; print "<TABLE Border = 1>\n<TH>Select me!</TH>"; my $THdone; while (my $result_ref = $sql->fetchrow_hashref()){ unless ($THdone) { foreach (sort keys %$result_ref) { print "<TH>$_ </TH>"; } $THdone = "yes, indeedy!"; } print "<TR><TD>",checkbox(-name=>"view",-checked=>"", -value=> +"$$result_ref{Name}"),"</TD>"; foreach (sort keys %$result_ref) { if (! defined $$result_ref{$_}) {print "<TD>--undef--</TD> +";next} print "<TD>$$result_ref{$_}</TD>"; } print "</TR>\n"; } print "</TABLE>"; print submit; print end_form; } print end_html;

Replies are listed 'Best First'.
(crazyinsomniac) Re: SP_HELP_DUMP
by crazyinsomniac (Prior) on Apr 21, 2001 at 18:06 UTC
    You know what sucks even more, not having sp_help and sp_columns (I found that out, right after I found out there was no DBI on the server, jeez.).

    Anyway, please provide us(me), the sp_* less monks with a working version. Since I can't test this, I don't guarantee it, not that I would anyway, but here's a few workarounds.

    First off I'd change line

    my $sql = $dbh->prepare ("sp_columns $_") || die;;
    to
    my $sql = $dbh->prepare ("SHOW COLUMNS FROM $_") || die;
    That should work without question. (note the single ; %^)

    And so should this

    my @tables = $dbh->ListTables; print "<TABLE Border = 1>\n<TH>Select me!</TH>"; foreach (@tables) { print "<TR><TD>",checkbox(-name=>"view",-checked=>"", -value=> +"$tables[$_]"),"</TD>"; if(! (defined $tables[$_]) ) { print "<TD>--undef--</TD>"; } else { print "<TD>$tables[$_]</TD>"; } print "</TR>\n"; }
    as opposed to
    my $sql = $dbh->prepare ("sp_help") || die;; my $sth = $sql->execute or die "no ".$DBI::errstr; print "<TABLE Border = 1>\n<TH>Select me!</TH>"; my $THdone; while (my $result_ref = $sql->fetchrow_hashref()){ unless ($THdone) { foreach (sort keys %$result_ref) { print "<TH>$_ </TH>"; } $THdone = "yes, indeedy!"; } print "<TR><TD>",checkbox(-name=>"view",-checked=>"", -value=> +"$$result_r +ef{Name}"),"</TD>"; foreach (sort keys %$result_ref) { if (! defined $$result_ref{$_}) {print "<TD>--undef--</TD> +";next} print "<TD>$$result_ref{$_}</TD>"; } print "</TR>\n";
    update:
    After further examination of your code, I decided to just substitute my $sql = $dbh->prepare ("sp_help") || die; with my $sql = $dbh->prepare ("SHOW TABLES") || die;
    But then I noticed yer code did not work as advertised. I ofcourse, knowing I couldn't get my money back, went ahead and "fixed" it. I just moved the "checkbox" statement inside the loop and(once in the loop), inside the if block.
    if (! defined $$result_ref{$_}) { print "<TD>",checkbox(-name=>"view",-checked=>"", -val +ue=>),"</TD>"; print "<TD>--undef--</TD>"; next; } print "<TD>",checkbox(-name=>"view",-checked=>"", -value=> +"$$result_ref{$_}"),"</TD>"; print "<TD>$$result_ref{$_}</TD>";
    You know, I was really suprised by this, especially coming from you boo.
    I wonder how many people blindly ++ed your post?

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"