Monday, March 14, 2022

[FIXED] How use CodeIgniter querybuilder with inserting data to MSSQL (newid() )

Issue

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.


Solution

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



Answered By - Tartarus

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.