MS SQL – Last Inserted ID
Notiz:
While trying to return a @@IDENTITY after an INSERT statement, the only way I could get a valid return result was to encapsulate the query within an EXEC like so:
$result = mssql_query(”
exec(”
INSERT INTO Files
(ownerId, name, sizeKB, path)
VALUES (‘$contactId’, ‘$userfile_name’, ‘$filesize’, ‘$path’)
SELECT @@IDENTITY as fileId”)
“);
list($fileId) = mssql_fetch_row($result);This returned the appropriate @@IDENTITY in a valid result set. Not sure if mssql supports multiple inline commands or not. But that assumption would back the useage of the EXEC command in order to execute these properly.
(fundstelle: PHP Resource)
March 28th, 2006 at 15:10
Hi
Genau das was ich gesucht habe!
Vielen Dank!
Harry
January 19th, 2007 at 10:42
mssql insert_id()
July 16th, 2007 at 13:02
June 11th, 2008 at 15:10
As you might have paid attention to, that ‘exec’ allows sql injections.
May 5th, 2009 at 15:15
I think you should use
SELECT scope_identity()