Connecting Tech Pros Worldwide Help | Site Map

Problem with Insert/update query with null values

  #1  
Old May 11th, 2009, 09:22 AM
Member
 
Join Date: Mar 2007
Posts: 94
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.
  #2  
Old May 11th, 2009, 10:42 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

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?
  #3  
Old May 11th, 2009, 11:43 AM
Member
 
Join Date: Mar 2007
Posts: 94

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.
  #4  
Old May 11th, 2009, 12:07 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

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.
  #5  
Old May 11th, 2009, 01:51 PM
Member
 
Join Date: Mar 2007
Posts: 94

re: Problem with Insert/update query with null values


It does!

Thanks acoder!
  #6  
Old May 11th, 2009, 02:01 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

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.
  #7  
Old May 11th, 2009, 02:04 PM
Member
 
Join Date: Mar 2007
Posts: 94

re: Problem with Insert/update query with null values


No problem - are there any tutorials on using it?
  #8  
Old May 11th, 2009, 02:20 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: Problem with Insert/update query with null values


Yes, there's a few. Here's one from the horse's mouth.
  #9  
Old May 12th, 2009, 10:06 AM
Member
 
Join Date: Mar 2007
Posts: 94

re: Problem with Insert/update query with null values


That's grand - thanks!
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with MERGE statement sumanroyc answers 1 May 2nd, 2007 12:45 PM
Bulk Insert / Update / Delete Philip Boonzaaier answers 16 November 11th, 2005 11:29 PM
Problem with Stored Procedure Jose Perez answers 2 July 20th, 2005 03:58 AM
Mysterious problem with INSERT query johnnyboy10017@yahoo.com answers 10 July 17th, 2005 10:55 AM