简体   繁体   中英

PHP PDO unixODBC FreeTDS SQL Server 'converting data type varchar(max) to datetime' in stored procedure

I have been banging my head against this for a while. I have Ubuntu 14.04 64bit with PHP 5.5.9-1ubuntu4.4 (cli) and have installed php5-odbc via apt-get, I have also installed freeTDS and unixODBC .

I am attempting to call a stored procedure by binding params as follows:

<?php

$dbh = new PDO('odbc:myDBSERVER', "username", "");

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function writestp($dbh){
    $dbh->beginTransaction();

    $sql = " EXEC
     stp_Update_Job 
     :Job_ID,
     :Modify_Date
     ";
    $sth = $dbh->prepare($sql);

    $params = array(
        ":Job_ID"                      => 123456,
        ":Modify_Date"                 => date(DateTime::ISO8601)
    );

    try{
        $sth->execute($params);
    }catch(Exception $e){
        echo $e->getMessage();
    }

    $dbh->rollback();
}

writestp($dbh);

?>

Which results in:

SQLSTATE[42000]: Syntax error or access violation: 8114 [FreeTDS][SQL Server]Error converting data type varchar(max) to datetime. (SQLExecute[8114] at /build/buildd/php5-5.5.9+dfsg/ext/pdo_odbc/odbc_stmt.c:254)[Finished in 0.1s]

UPDATE This fixes it:

    $params = array(
        ":Job_ID"                      => 123456,
        ":Modify_Date"                 => date("Y-m-d\TH:i:s")
    );

ENDUPDATE :

My stored proc starts like this:

CREATE PROCEDURE [dbo].[stp_Update_Job]
                @Job_ID                         int,
                @Modify_Date                    datetime

Config files as follows:

/etc/odbinst.ini

[FreeTDS]
Description = Free TDS driver
# Driver on Ubuntu 64 bit
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

/etc/odbc.ini

# set a DSN
[myDBSERVER] # you can now refer to this connection 
                    # by this name when connecting via odbc.

# Some info about the connection
Description = Connection to myDBSERVER

# name of the odbc driver to use as specified in /etc/odbcinst.ini
Driver      = FreeTDS

# Database name to connect to
Database    = myDBName

# Server name as specified in /etc/freetds/freetds.conf
ServerName  = myDBSERVER
TDS_Version = 7.2

/etc/freetds/freetds.conf

[myDBSERVER]
    host = myDBSERVERHOST
    port = 1433
    tds version = 7.2
    client charset = UTF-8

It's quite frustrating to realise that despite this page suggesting the ISO8601 is a supported date time format, in fact, the timezone part to the date is NOT supported.

http://msdn.microsoft.com/en-us/library/ms187819.aspx

Where I was using date(DateTime::ISO8601) under the covers, this uses the PHP date format string: "Ymd\\TH:i:sO" .

Using "Ymd\\TH:i:s" instead, fixes this!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM