Home:ALL Converter>CodeIgniter MSSQL get last_id() of composite primary key

CodeIgniter MSSQL get last_id() of composite primary key

Ask Time:2018-06-08T22:02:04         Author:guyfromfl

Json Formatter

I am working on an application for our company that requires a composite primary key.

Per the requirements, the composite is made up of RegionID and AuthorizationID but AuthroizationID does not need to be unique to itself. In other words they want it to do this:

RegionID    AuthorizationID
300         1
300         2
100         1

So I can't create a simple Identity on Authorization then just stick RegionID on the front. (Believe me I tried my best to not make it this way)

Anyway, I created an INSERT trigger in MSSQL 2008 that that OUTPUTs the newly inserted RegionID and AuthorizationID, and encapsulated it in dbo.vwCreateRA. It works great in MSSQL Studio.

However to make it at all usable, I want to be able to get the Composite PK back from an insert inside codeigniter, to redirect the user to the newly created record.

$sql = "INSERT INTO ReturnAuthorizations.dbo.vwCreateRA (
                    /* a bunch of other fields... */
                /* a bunch of other values */

    $query = $this->db->query($sql, [
        /* A bunch of other variables... */


Gives me: Fatal error: Call to a member function last_id() on a non-object

How can I grab the OUTPUT from the view's INSERT through CodeIgniter, or will I have to rely on something like:

(Please excuse the code, probably not best practices, only intended to make the point of the query)

$sql = "SELECT TOP 1
        RegionID = " . $customer['Region'] . "
        AND UserID = " . $username . "
        CreateDate DESC";


Author:guyfromfl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762380/codeigniter-mssql-get-last-id-of-composite-primary-key