CFLib.org – Common Function Library Project

MSSQLCreateUUID([format])

Last updated June 27, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DatabaseLib

 
Rated 0 time(s). Average Rating: 0

Description:
CF UUIDs are formatted differently than MSSQL UUIDs generated with the T-SQL function newid(). This can lead to problems when working with UUIDs. This function can return a UUID formatted like newid() in either string or binary format. String values must be quoted when inserted into the database whereas binary values do not have to be quoted. The latter is of help when one wants to have either a UUID or NULL inserted- neither of which is quoted. Takes an optional parameter indicating output format which can be either 'string' or 'binary'.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
cfquery ....<BR>
   Insert into myTable<BR>
   (myTableID, name)<BR>
    VALUES<BR>
   (#MSSQL_createUUID('binary')#,'Joe Bloggs')<BR>
/cfquery<BR><BR>
<P>
cfquery ....<BR>
   Insert into myTable<BR>
   (myTableID, name)<BR>
    VALUES<BR>
   ('#MSSQL_createUUID('string')#','Joe Bloggs')<BR>
/cfquery<BR>
<P>
cfquery ....<BR>
   Insert into myTable<BR>
   (myTableID, name)<BR>
    VALUES<BR>
   ('#MSSQL_createUUID()#','Joe Bloggs')<BR>
/cfquery
</cfoutput>

Parameters:

Name Description Required
format UUID format to generate. Options are String or Binary. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Creates UUIDs safe for use in MSSQL UNIQUEIDENTIFIER fields.
 * 
 * @param format      UUID format to generate.  Options are String or Binary. (Optional)
 * @return Returns a string. 
 * @author Chip Temm (chip@anthrologik.net) 
 * @version 1, June 27, 2002 
 */

function MSSQL_createUUID(){
    var format = 'string';
    // uniqueidentifier wombat_createUUID([string FORMAT])
    //returns a UUID in the format specified.  
    //optional argument FORMAT defaults to string (MS-SQL uniqueidentifier safe)
    //accepts 'binary' or 'string'.  other values fail quietly to 'string'
    
    
    if(arraylen(Arguments)){
        if(arguments[1] eq 'binary' or arguments[1] eq 'string'){
            format = arguments[1];
        }
    }
    
    if(format eq 'string'){
        return Insert("-"CREATEuuid(), 23);
        /***   NOTE quoted usage is SQL statement:
        Insert into attribute (attributeID) values ('#wombat_createUUID()#')
        ***/

    
    }else{//must be raw binary
        return '0x' & Replace(CREATEuuid(),'-','','All'); 
        /***   NOTE UN-quoted usage is SQL statement:
        Insert into attribute (attributeID) values (#wombat_createUUID('binary')#)
        Good for cases where the value maybe either NULL or a UUID
        (neither of which should be quoted)
        ***/

    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson