MSSQL INSERT query failing for a specific table wh

2019-09-06 15:09发布

问题:

I need to read the date that a request was created from our website. When that request is created, the information corresponding to that request and its meta-request is inserted in the DAI_REQ.REQUEST and DAI_REQ.META_REQUEST tables, respectively. We also have a dev server and a public deployment server. The problem happens only on our deployment server for some reason..

Unfortunately, the INSERT query to insert the information of the meta-request in the DAI_REQ.META_REQUEST table does not work, but the SELECT query I do right after does (so in my eyes, this removes any connection problems with the database/table itself). I also use the same syntax as the INSERT query I do on the DAI_REQ.REQUEST, so I do not think it is a query syntax problem. I also tried manually inserting as line within sql-server and it works fine. Finally, I echo'ed the value of $this->userId that I use as a parameter for the INSERT query to see if it contained the right ID, and it does. I did the same for the return value of $this->db->query(...), and it does NOT return anything (on our deployment server only).

I also know that my way of retrieving the last inserted row in a table is not perfect, but this is not the problem at hand here and it will be changed later on.

Here is the actual code where the problem happens:

public function dbInsert(){

    // The actual problematic query
    $this->db->query("INSERT INTO DAI_REQ.META_REQUEST ".
        "(DATE_RECU, DATE_TERMINEE, USER_ID, STATUS) ".
        "VALUES(GETDATE(), '', ?, 'R');", array($this->userId));

    // This works fine though
    $mr_select = $this->db->query("SELECT TOP 1 ID FROM DAI_REQ.META_REQUEST WHERE USER_ID = ? ORDER BY ID DESC;",
            array($this->userId));
    $mr_result = $mr_select->result_array();
    $mr_id = $mr_result[0]['ID'];

    $sim = 'N/A';
    if(isset($this->recurrenceType))
        $sim = 'Recurrent';

    $this->db->query("INSERT INTO DAI_REQ.REQUEST ".
        "(USER_ID, ASSIGNED_DATE, REQUEST_END_DATE, MODEL, EXPERIMENT, VARIABLE, START_DATE, END_DATE, ".
        "LON_FROM, LAT_FROM, LON_TO, LAT_TO, RESOLUTION, FORMAT, SIMULATION, STATUS, ".
        "CANCELLED_YN, PROJECT, MR_ID, URL_ORIGIN, DATE_EMAIL) ".
        "VALUES(?, GETDATE(), '', ?, 'N/A', 'N/A', ?, ?, ?, ?, ?, ?, ?, ?, ?, 'R', 0, 'N/A', ?, ?, ?);",
        array($this->userId, $this->model, $this->startDate, $this->endDate, 
                $this->lonFrom, $this->latFrom, $this->lonTo, $this->latTo, 
                $this->resolution, $this->format, $sim, $mr_id, $this->url_origin, $this->date_email));

    $r_select = $this->db->query("SELECT TOP 1 ID FROM DAI_REQ.REQUEST WHERE USER_ID = ? ORDER BY ID DESC;",
            array($this->userId));
    $r_result = $r_select->result_array();
    $this->id = $r_result[0]['ID'];
}

回答1:

The database that the deployment server is using isn't set up to auto increment the ID column. In Microsoft SQL Server, for the ID column, you can set the Identity to Yes and Identity Increment to whatever number you want the ID column to increment by.