Skip to main content
Participant
November 1, 2007
Answered

insert null in to unique identifier

  • November 1, 2007
  • 3 replies
  • 1598 views
Hi i am trying to insert a null value in to a sql server database.

My calling line is

<cfset VARIABLES.QRYTEST = Application.dbbatchUpdate.test(variables.bupd.dsnC, '')>

and my function to unsert the value is

<cffunction name="test" access="public" output="false" returntype="boolean">
<cfargument name="dsn" type="string" required="yes" />
<cfargument name="test" type="guid" required="no" />
<cfset var local = structnew()>

<cfquery name="local.test" datasource="#arguments.dsn#">
update tb_problem
set itemid = <cfqueryparam cfsqltype="cf_sql_idstamp" value="#arguments.test#">
where problemnumber = 'CEN295'
</cfquery>

<cfreturn true >
</cffunction>

i keep getting the error "element test passed to function is not of type guid".

I have also tried changing the argument type to any, and the cf_sql_type to be a char but still no luck .

any ideas on how to solve this.

cheers

stewart
This topic has been closed for replies.
Correct answer cf_dev2
stewart_smith1 wrote:
> <cfset VARIABLES.QRYTEST = Application.dbbatchUpdate.test(variables.bupd.dsnC, '')>
> i keep getting the error "element test passed to function is not of type guid".

Because an empty string '' is not a valid guid.


> also tried changing the argument type to any, and the cf_sql_type to be a char but still no luck .

That would work for CF. But assuming your column type is uniqueidentifier, MS SQL would throw an error because an empty string '' is not a valid uniqueidentifier


> <cfargument name="test" type="guid" required="no" />
> <cfqueryparam cfsqltype="cf_sql_idstamp" value="#arguments.test#">

That will cause an error if nothing was passed in for arguments.test


> i am trying to insert a null value in to a sql server database

I'm assuming "itemID" is not your table's primary key since you're trying to set the value to NULL. If it were the PK, that would be troubling ... ;-)

Anyway, you need to read up on cfqueryparam's "null" attribute. If null="true" or null= "yes" a NULL value will be passed to the database. If you want really want to pass a empty string "" to your function, you could skip the type checking and use type="any". Then use "null" in your cfqueryparam

<cfargument name="test" type="any" required="no" default="" />
...
<cfqueryparam cfsqltype="cf_sql_idstamp" value="#arguments.test#" null="#not len(trim(arguments.test))#">

Or you could keep the type checking and make the argument optional. Then inside the function, set a default value if the UUID was not passed.

3 replies

cf_dev2Correct answer
Inspiring
November 2, 2007
stewart_smith1 wrote:
> <cfset VARIABLES.QRYTEST = Application.dbbatchUpdate.test(variables.bupd.dsnC, '')>
> i keep getting the error "element test passed to function is not of type guid".

Because an empty string '' is not a valid guid.


> also tried changing the argument type to any, and the cf_sql_type to be a char but still no luck .

That would work for CF. But assuming your column type is uniqueidentifier, MS SQL would throw an error because an empty string '' is not a valid uniqueidentifier


> <cfargument name="test" type="guid" required="no" />
> <cfqueryparam cfsqltype="cf_sql_idstamp" value="#arguments.test#">

That will cause an error if nothing was passed in for arguments.test


> i am trying to insert a null value in to a sql server database

I'm assuming "itemID" is not your table's primary key since you're trying to set the value to NULL. If it were the PK, that would be troubling ... ;-)

Anyway, you need to read up on cfqueryparam's "null" attribute. If null="true" or null= "yes" a NULL value will be passed to the database. If you want really want to pass a empty string "" to your function, you could skip the type checking and use type="any". Then use "null" in your cfqueryparam

<cfargument name="test" type="any" required="no" default="" />
...
<cfqueryparam cfsqltype="cf_sql_idstamp" value="#arguments.test#" null="#not len(trim(arguments.test))#">

Or you could keep the type checking and make the argument optional. Then inside the function, set a default value if the UUID was not passed.
Inspiring
November 2, 2007
Yes, you can't insert null to a primary key. I haven't tested this, but have you tried defaulting your test argument?:

<cfargument name="test" type="guid" required="no" default="#CreateUUID()#" />
Inspiring
November 1, 2007
Primary key fields can't be null. What are you trying to accomplish by nullifying it?