Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with Insert/update query with null values

Member
 
Join Date: Mar 2007
Posts: 94
#1: May 11 '09
Hi,

My database user has just decided that a recent text field in my database needs to be numeric so that some calculations can be run. I've changed it to a double so I can keep the point formatting (ie. 4.1).

I have changed the data types and altered my variables to insert numbers. I have added a default value of 0 to new records added to the database.

Howevern, some of the existing records have a null field in this changed column.
CF can display them but won't update the field without erroring out if the field is left blank.

Expand|Select|Wrap|Line Numbers
  1. PowerRating = <cfif IsDefined("form.PowerRating") AND #form.PowerRating# NEQ "">
  2.         #form.PowerRating#
  3.         <cfelse>
  4.         NULL
  5.         </cfif>,
Any idea what I am doing wrong?
Thanks
Neil
best answer - posted by acoder
The following code:
Expand|Select|Wrap|Line Numbers
  1. PowerRating = <cfif IsDefined("form.PowerRating")>
  2.     <cfif #form.PowerRating# NEQ "">
  3.         <cfset PowerRating = #form.PowerRating#>
  4.     <cfelse>
  5.         NULL
would result in the output "PowerRating = " when there's a value because you've set a variable but you haven't output anything. Your original code would work.

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 11 '09

re: Problem with Insert/update query with null values


What's the error message? Have the NULL values in the existing fields been set by the query or the default used to be null?
Member
 
Join Date: Mar 2007
Posts: 94
#3: May 11 '09

re: Problem with Insert/update query with null values


Quote:

Originally Posted by acoder View Post

What's the error message? Have the NULL values in the existing fields been set by the query or the default used to be null?

It's a basic sytnx error:

ODBC Error Code = 37000 (Syntax error or access violation)
[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.

The null values are set by the query.
This is the code that adds a new record:

Expand|Select|Wrap|Line Numbers
  1. <cfif IsDefined("form.PowerRating") AND #form.PowerRating# NEQ ""> 
  2.     #form.PowerRating# 
  3.       <cfelse> 
  4.     NULL 
  5.   </cfif>,
This is the code that updates it if their is a change:

Expand|Select|Wrap|Line Numbers
  1. PowerRating = <cfif IsDefined("form.PowerRating")>
  2.     <cfif #form.PowerRating# NEQ "">
  3.         <cfset PowerRating = #form.PowerRating#>
  4.     <cfelse>
  5.         NULL
  6.     </cfif>
..which works if the valeu is NULL, but not if it has retrieved any other value.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: May 11 '09

re: Problem with Insert/update query with null values


The following code:
Expand|Select|Wrap|Line Numbers
  1. PowerRating = <cfif IsDefined("form.PowerRating")>
  2.     <cfif #form.PowerRating# NEQ "">
  3.         <cfset PowerRating = #form.PowerRating#>
  4.     <cfelse>
  5.         NULL
would result in the output "PowerRating = " when there's a value because you've set a variable but you haven't output anything. Your original code would work.
Member
 
Join Date: Mar 2007
Posts: 94
#5: May 11 '09

re: Problem with Insert/update query with null values


It does!

Thanks acoder!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: May 11 '09

re: Problem with Insert/update query with null values


Just a point to bear in mind: in queries where you accept user input as values, you should use cfqueryparam to insert/update values in the database.
Member
 
Join Date: Mar 2007
Posts: 94
#7: May 11 '09

re: Problem with Insert/update query with null values


No problem - are there any tutorials on using it?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: May 11 '09

re: Problem with Insert/update query with null values


Yes, there's a few. Here's one from the horse's mouth.
Member
 
Join Date: Mar 2007
Posts: 94
#9: May 12 '09

re: Problem with Insert/update query with null values


That's grand - thanks!
Reply