Getting ID from last inserted record

Hi, i'm inserting rows in a table using a stored procedure, but i need the autogenerated ID(by SQL server 2005) from the inserted row as a FK to insert other rows. How is it possible to get that autogenerated ID

Greetz



Answer this question

Getting ID from last inserted record

  • lee d

    Nevermind I already found the solution You have to add this in your stored procedure

    DECLARE @NEWID bigint

    SET @NEWID= (SELECT SCOPE_IDENTITY());

    RETURN @NEWID

    You also add a SqlParameter object in your c# program

    //FCID ID

    SqlParameter newid= new SqlParameter();

    newid.Direction = System.Data.ParameterDirection.ReturnValue;

    newid.ParameterName = "@NEWID";

    objCmd.Parameters.Add(newid);

    Finally you want to get the returnvalue from the stored procedure by doing this:

    SqlParameter newid= objCmd.Parameters["@NEWID"];

    myid = newid.Value.ToString();

    Greetz


  • Getting ID from last inserted record