我有這段代碼將在我的表上插入一些數據,並且我需要獲取最後插入的ID,這是我的PK。使用PHP在SQL Server上插入的最後一個值
$serverName = "MyServer";
$connectionInfo = array("Database"=>"MyBD", "UID"=>"MyUser", "PWD"=>"MyPass");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn)
{
echo "Connection established.<br />";
}
else
{
echo "Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
$nome = $_POST["nome"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$unidade = $_POST["unidade"];
//declare the SQL statement that will query the database
$query = "INSERT INTO [dbo].[Participante] ([nome],[email],[telefone],[unidadeCE],[dataCadastro]) VALUES ('" . $nome . "', '" . $email . "', '" . $telefone . "', '" . $unidade . "', (getdate())); SELECT @@IDENTITY";
//execute the SQL query and return records
$result = sqlsrv_query($conn, $query);
var_dump($result);
//close the connection
sqlsrv_close($conn);
我已經嘗試過SELECT @@IDENTITY
但它只返回resource(4) of type (SQL Server Statement)
使用SCOPE_IDENTITY() – HaveNoDisplayName 2014-12-09 13:18:24
@Jordy,對不起,我沒有找到!謝謝:) – Terkhos 2014-12-09 13:33:18