MS SQL/FreeTDS failing with a simple SQL SET

Chris <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
Hi,

I have identified a problem with a MS SQL query I am running with the 
freetds driver, PDO and PHP.

Basically, if I send the query:

DECLARE @A INT
SET @A = 1
SELECT 2 AS b

It fails. But if I remove the SET it works. The code looks like this:

<?php

$old_dbhost="somehost";
$old_dbuser="auser";
$old_dbpass="topsecret";
$old_database="database";

try {
$dbh = new PDO("dblib:host=$old_dbhost;dbname=$old_database", 
$old_dbuser, $old_dbpass);
} catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br/>";
exit();
}

// Uncomment the SET below, and the query will work

$sql="DECLARE @A INT
-- SET @A = NULL
SELECT 2 AS B
";

try {
$sth=$dbh->prepare("$sql");
} catch (PDOException $e) {
echo "prepare Error!: " . $e->getMessage() . "<br/>";
}

try {
$sth->execute();
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

try {
$records=$sth->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
unset($sth);

print_r($records);

?>

When it fails (with the SET uncommented) the freetds log contains:

11:33:45.967347 3683 (dblib.c:4565):dbsqlok() not done, calling 
tds_process_tokens()

But with the SET in place, the log contains:

11:32:18.751088 3671 (dblib.c:4556):dbsqlok() exits on result token 0xa0

I think this would suggest a syntax error with the SQL, but I have run 
it directly on the server, and it works fine.

Versions:
FreeTDS 0.82
SQL Server: 2008 R2
PHP: 5.3.3

Any ideas anyone?

Cheers,

Chris.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.