Advertisement
jchaven

Insert NULL in UNIQUEIDENTIFIER field

Jan 17th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. /*
  2. How to insert a known UUID/GUID (record_id) - in this case generated by PHP, an unknown or blank UUID/GUID, and a regular int field
  3.  
  4. SQL Server Table creation code:
  5.  
  6. CREATE TABLE dbo.MYTable
  7.     (
  8.     [id] UNIQUEIDENTIFIER NOT NULL,
  9.     [core_uuid] UNIQUEIDENTIFIER NULL,
  10.     [category_id] UNIQUEIDENTIFIER NULL,
  11.     [other_id] INT NOT NULL DEFAULT 0,
  12.     PRIMARY KEY NONCLUSTERED (id)
  13.     );  
  14.  
  15. */
  16.  
  17. // fix zero/null fields
  18. $record_id = 'C39A4043-7DF9-4FCB-B5F5-C47F813A97B5';
  19. $core_uuid = (!empty($core_uuid)) ? "'$core_uuid'" : "NULL";
  20. $category_id = (!empty($category_id)) ? "'$category_id'" : "NULL";
  21.                
  22. // setup query  for MS SQL Server          
  23. $query = 'INSERT INTO [database].[schema].[table] ([id], [core_uuid], [category_id], [other_id]) '
  24.   . 'VALUES (\''.$record_id.'\','.$core_uuid.','.$category_id.','.$other_id.')';
  25.                
  26. // execute query
  27. $result = mssql_query($query); 
  28. if (!$result) {
  29.     // query failed, print error to log
  30.     $err = mssql_get_last_message();
  31.     $log->lwrite('--> MSSQL error: '.$err);    
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement