0

PHP と Zend Framework を使用して Web サイトを作成しています。データベースと通信するために sqlsrv アダプターを使用しています。挿入クエリを作成し、新しい行の ID も保存しようとしています。次のような SQL クエリを作成しました。

$db = Zend_Db_Table::getDefaultAdapter();               
$id = $db->fetchOne("
    INSERT INTO    MyTable
    (Title, CreatedBy, Created, LastUpdatedBy, LastUpdated)
    SELECT  'MY Title',
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["Created"]."', 120),
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["LastUpdated"]."', 120)
    FROM    UserTable
    WHERE   UserName = '".$this->details["CreatedBy"]."'
    SELECT  CAST(SCOPE_IDENTITY() AS int) AS Id"
);

しかし、それはこのエラーを生成します:

'Zend_Db_Statement_Sqlsrv_Exception' with message 'The active result for the query contains no fields.

このエラーを回避してこのクエリを実行し、挿入された行 ID を取得するにはどうすればよいですか?

4

1 に答える 1

1

ここで間違った方法を使用しているようです。

Zend_Db_Tableのメソッドを使用する必要がありますinsert() :-

$table = new Zend_DB_Table(array('name' => 'mytable', primary => 'myPrimaryKey'));
$data = array()// of data to be inserted
$id = $table->insert($data);
于 2012-06-06T10:50:19.393 に答える