MSSQL获取INSERTED ID并使用字符串PHP返回它

时间:2015-12-10 12:51:02

标签: php sql-server

你好我有工作mssql php插入代码,但我想插入id返回字符串($ newid)我的代码在。

OrderId = int且主键不为空

$querysf = "SELECT TOP 1 OrderId FROM Orders ORDER BY OrderId DESC";
$resultsf = mssql_query($querysf);

//Getting manually latest OrderId but this is not good :(

while($rowsf = mssql_fetch_array($resultsf))
{
    $newid = $rowsf["OrderId"];
    $newid = $newid + 1;

    echo("$newid"); //Getting manually latest OrderId but this is not good :( Some one can easily take this id and will be mistakes

    $mydate = date("Y-m-d");

    $ready = mssql_fetch_assoc(mssql_query("SET IDENTITY_INSERT Orders ON; SELECT CONVERT(NCHAR(100),NEWID()) as Ok"));

    $queryer = mssql_query("INSERT INTO Orders (OrderId, CustomerId, DeliveryDate, OrderState,Signature,UserName,special_field,rowguid) VALUES ('$newid', '$CustomerId', '$mydate 00:00:00.000', '1',NULL,'$UserName',NULL,'".$ready['Ok']."')"); 

    $close = mssql_fetch_assoc(mssql_query("SET IDENTITY_INSERT Orders OFF;"));

}

0 个答案:

没有答案