Home > Software design >  How use CodeIgniter querybuilder with inserting data to MSSQL (newid() )
How use CodeIgniter querybuilder with inserting data to MSSQL (newid() )

Time:09-21

For id i use uniqueidentyfier, and in queries insert i write: newid() in, but, how do this in Query builder?

{
$db = \Config\Database::connect();

$data = [
 'id'  =>  'newid()',
 'id_zgloszenia'  =>  $idpp,
 'response'  => $response_pp,
 'header_response' => $head_res, 
 'data_datetime' => 'getdate()',   
];

$builder = $db->table('DOM5_PP_LOGI'); 

 $builder->insert($data);     

}  

I try like above, and also: $builder->set('id', uniqid());

But i had error: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier.

CodePudding user response:

You can try this to generate UUID by MSSQL:

$builder->set('id', 'NEWID()', FALSE);

Explain: set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. To illustrate the difference, here is set() used both with and without the escape parameter.

You can get find out more here: https://codeigniter.com/userguide3/database/query_builder.html#id8

  • Related