Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: nested dbi queries question

by trammell (Priest)
on Feb 01, 2005 at 22:58 UTC ( #427088=note: print w/replies, xml ) Need Help??


in reply to nested dbi queries question

I see at least two problems. I think you want s/&&/AND/ in this SQL:
INSERT INTO sp_err SELECT * FROM sp WHERE spnum = $spnum && snum = $snum
and your $sth variables will clash. How about $sth2?

Replies are listed 'Best First'.
Re^2: nested dbi queries question
by philosophia (Sexton) on Feb 01, 2005 at 23:28 UTC
    i tried the code below, no errors, but it's also not moving rows from table sp to table sp_err

    # begin fix loop

    $sql = "SELECT * FROM sp WHERE NOT EXISTS (SELECT * FROM SP,SUPPLIER WHERE SUPPLIER.snum = SP.snum)";
    $sth2 = $dbh->prepare($sql);
    $sth2->execute or die "Error: $DBI::errstr\n";

    $sth2->bind_col(1,\$spnum);
    $sth2->bind_col(2,\$snum);
    $sth2->bind_col(3,\$pnum);
    $sth2->bind_col(4,\$qty);

    while($sth->fetch){
    $sql = "INSERT INTO sp_err SELECT * FROM sp WHERE spnum = $spnum && snum = $snum";
    $sth = $dbh->prepare($sql);
    $sth->execute or die "Error: $DBI::errstr\n";
    print "deleted row";

    }
      Well you should have errors--you're fetching from $sth before you execute it, then clobbering it inside your while loop.
        here's my current code. same result still...

        # begin fix loop

        $sql = "SELECT * FROM sp WHERE NOT EXISTS (SELECT * FROM SP,SUPPLIER WHERE SUPPLIER.snum = SP.snum)";
        $sth = $dbh->prepare($sql);
        $sth->execute or die "Error: $DBI::errstr\n";

        $sth->bind_col(1,\$spnum);
        $sth->bind_col(2,\$snum);
        $sth->bind_col(3,\$pnum);
        $sth->bind_col(4,\$qty);

        while($sth->fetch){

        $sql2 = "INSERT INTO sp_err SELECT * FROM sp WHERE spnum = $spnum && snum = $snum";
        $sth2 = $dbh->prepare($sql2);
        $sth2->execute or die "Error: $DBI::errstr\n";
        print "deleted row";

        }
      i think i've narrows the problem to this line
      $sql = "SELECT * FROM sp WHERE NOT EXISTS (SELECT * FROM SP,SUPPLIER WHERE SUPPLIER.snum = SP.snum)";
      it's not finding any results. it's supposed to find all rows in table sp where sp.snum does not exist in table supplier

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://427088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-06-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (81 votes). Check out past polls.

    Notices?