0

I'm using this code to insert new record to database:

function newProtocol($date, $type, $text, $who, $reff) {
    $sql = "INSERT INTO c_rozhovor (ref_kunde, protokol_typ, besuch_protokol, datum_vzniku, kto_vytvoril) VALUES ('"
            .msescape($reff)."','"
            .msescape($type)."','"
            .msescape($text)."','"
            .msescape($date)."','"
            .msescape($who)."')";

    $connection = odbc_connect(constant('DATABASE_NAME'),'','');
    if (!connection) return false;      

    $query = odbc_exec($connection, $sql);
    if (!query) return false;

    return "New protocol was successfuly saved!";   
}

I would like somehow to return last inserted id instead of New protocol was successfully saved! string. Something like LAST_INSERT_ID() in MySql. Is there a way to achieve that in SQL Server 2000?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Axel Stone
  • 1,521
  • 4
  • 23
  • 43

1 Answers1

-1

Can you try the following modification of your code:

function newProtocol($date, $type, $text, $who, $reff) {
    $sql = "INSERT INTO c_rozhovor (ref_kunde, protokol_typ, besuch_protokol, datum_vzniku, kto_vytvoril) VALUES ('"
            .msescape($reff)."','"
            .msescape($type)."','"
            .msescape($text)."','"
            .msescape($date)."','"
            .msescape($who)."') SELECT SCOPE_IDENTITY()";

    $connection = odbc_connect(constant('DATABASE_NAME'),'','');
    if (!connection) return false;      

    $query = odbc_exec($connection, $sql);
    if (!query) return false;

    return $query;
}
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44