?
Solved

Insert into SQL table

Posted on 2002-06-20
4
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 7097175
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
ID: 7097310
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
ID: 7097325
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
ID: 7097348
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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