Solved

Insert into SQL table

Posted on 2002-06-20
4
166 Views
Last Modified: 2013-12-24
Hell Experts,

I'm having a little difficulty doing something I've done many times before.  I, currently inserting into a SQL database with 4 fields.  To of them can be null and two can't.  The ICWAEmpID is a number and the other three are text fields.  Below is my SQL and below that is the error I get when I don't put a value into one or both of the not required fields (which can be null).  Any ideas?


My code:
<cfquery name="InsertICWA" datasource="ddssprojdev" dbtype="ODBC">
          INSERT INTO ICWA (Salco, ICWANO, BudgetNo, ICWAEmpID)
          VALUES ('#FORM.Salco#','#FORM.ICWANO#', '#FORM.BudgetNo#', #FORM.SelectOwner#);
          </cfquery>
Error Diagnostic Information
ODBC Error Code = 37000 (Syntax error or access violation)


[Microsoft][ODBC SQL Server Driver][SQL Server]Line 2: Incorrect syntax near ')'.


SQL = "INSERT INTO ICWA (Salco, ICWANO, BudgetNo, ICWAEmpID) VALUES ('333333', '11111', '', )"

Data Source = "DDSSPROJDEV"


The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (53:3) to (53:68) in the template file I:\WEBSITES\DDSSPM2\UPDATE\ADDICWA_APP.CFM.

0
Comment
Question by:tdenny
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
Comment Utility
this is what you can do:

<cfquery name="InsertICWA" datasource="ddssprojdev" dbtype="ODBC">
         INSERT INTO ICWA (Salco, ICWANO, BudgetNo, ICWAEmpID)
         VALUES ('#FORM.Salco#','#FORM.ICWANO#', '#FORM.BudgetNo#', <cfif IsDefined("FORM.SelectOwner") and LEN(TRIM(FORM.SelectOwnder)) GT 0>#FORM.SelectOwner#<cfelse>null</cfif>)
         </cfquery>


do the same for the other field that allows null.

If that doesn't work use the same type of if then else structure to adjust the query to omit the fields that are null.

CJ
0
 

Author Comment

by:tdenny
Comment Utility
CJ,

Thanks as always for your response.  I thought there might be some sort of isNull requirement for my SQL statement.  I am sure your suggestions will work.  Your suggestion is certainly better than running two separate queries based on whether or not the field is used.  I will do the same for both of those fields.  I can't wait until I get to the really big table that this application will be inserting recdords into.  

Many thanks
TD
0
 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
if the query gets too big, I would suggest creating a stored procedure for inserting new rows.  And make all the fields that allow null values optional parameters.

Then you don't need to do the whole query building just use CFStoredProc tag.

also, this approach is probably a better way to manage larger inserts

Thanx for the "A"

CJ
0
 

Author Comment

by:tdenny
Comment Utility
CJ,

Thanks as always for your response.  I thought there might be some sort of isNull requirement for my SQL statement.  I am sure your suggestions will work.  Your suggestion is certainly better than running two separate queries based on whether or not the field is used.  I will do the same for both of those fields.  I can't wait until I get to the really big table that this application will be inserting recdords into.  

Many thanks
TD
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IIS 8.5 2 44
Change local server setting in php 6 59
Configure IIS to process JSON 10 61
.dwt files not viewable in browser - why? 2 49
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now