my $sql = qq(set @match = (SELECT count(number) FROM test where number='22'); if (@match = '0') insert into test values(4); select @match; select * from test;); my $sth = $dbh->prepare($sql); $sth->execute();