dongnai8013 2012-09-26 13:32
浏览 46

sqlserver + php:刚刚插入行id

I tried this way:

$result = odbc_exec($connection, 'SELECT id FROM MyTable WHERE id = @@Identity');

but it gives me

syntax error: unexpected T_VARIABLE

edit: here's complete logic thanks to your help:

$result = odbc_exec($connection, 'INSERT data into the table;SELECT SCOPE_IDENTITY() AS id;');
$row = odbc_fetch_array($result); //this line gives the error!
$id = $row['id']; 

edit2: I missed a ";" =_=

Anyway scope_identity does not work: gives

no tuples available at this index

on fetcharray call

  • 写回答

3条回答 默认 最新

  • douwen1929 2012-09-26 13:33
    关注

    After inserting a row, you can look up the last inserted id using scope_identity(). There's no need to look it up in the original table:

    SELECT SCOPE_IDENTIY()
    

    If the row was inserted on another connection, you can query the maximum value of id:

    SELECT max(id) FROM MyTable
    
    评论

报告相同问题?