#!/usr/bin/perl use DBI; my @connection = ("DBI:mysql:database=notexiststest;host=localhost;port=3306", "testuser", "*******"); my $distinct_query = 'INSERT INTO t1 (f1) SELECT @x:=? FROM DUAL WHERE NOT EXISTS ( SELECT * FROM t1 WHERE f1=@x)'; for (my $i=0;$i<2;$i++) { my $dbh = DBI->connect(@connection); my $distinct_query_handle = $dbh->prepare($distinct_query) or die("SQL ERROR: ".$dbh->errstr); $distinct_query_handle->execute(2) or die("SQL ERROR: ".$dbh->errstr); }