How do I pass null values in an Insert statement to an oracle database?

Posted on 2008-11-10
Medium Priority
Last Modified: 2013-12-07
I have this insert statement and I'm getting errors because it is not handling columns where null values are permitted...

strInsert = "INSERT INTO DRAWING" & _
                  "( DRAWING_ID, " & _
                  " DRAWING_NUMBER, " & _
                  " DRAWING_TYPE, " & _
                  " REVISION, " & _
                  " ISSUE_DATE) " & _
                  "VALUES " & _
                  "(" & drDrawing.Item("DRAWING_ID").ToString & "," & _
                          drDrawing.Item("DRAWING_NUMBER").ToString & "," & _
                          drDrawing.Item("DRAWING_TYPE").ToString & "," & _
                          drDrawing.Item("REVISION").ToString & "," & _
                          drDrawing.Item("ISSUE_DATE").ToString & ")"

The statement is passing values from the current data row object to a web service for execution. The problem is there is an error with this statement. I noticed that the only required column is Drawing_ID...if any of the other columns are passing a null then the whole block crashes.

What is the syntax for handling a null column? Any help would be appreciated...or enen a link in the right direction. I am on google right now trying to figure this out.

Question by:imonfireDAMMIT
  • 3
  • 2
LVL 74

Expert Comment

ID: 22925076
use bind variables instead of string concatenation.

or, if you want to put in null...

strInsert = "INSERT INTO DRAWING" & _
                  "( DRAWING_ID, " & _
                  " DRAWING_NUMBER, " & _
                  " DRAWING_TYPE, " & _
                  " REVISION, " & _
                  " ISSUE_DATE) " & _
                  "VALUES " & _
                  "(" & drDrawing.Item("DRAWING_ID").ToString & ",NULL,NULL,NULL,NULL)"

Open in new window

LVL 74

Accepted Solution

sdstuber earned 1500 total points
ID: 22925091
if you use bind variables,  it won't matter if any of the items are NULL or not, nor will you have to convert them to strings either.  You can pass strings as strings, dates as dates, numbers as numbers and nulls as nulls.

String concatenation also opens your application to sql injection.

Author Closing Comment

ID: 31515223
I decided to use a string builder object and then pass the string builder to a string variable. This way I could pass the string to the webmethod.  THanks for your help I see that string concatenation was not my best bet

Author Comment

ID: 22930458
I decided to use a string builder to create my string...this made it easier to handle columns where nulls could be accepted because I could place If statements inside my string builder object.
LVL 74

Expert Comment

ID: 22930857
string builder is still string concatenation and still open to sql injection.

Glad you got it to work, but you'll really benefit in the long run if you switch to using bind variables instead of embedded literals.  Your applications will run faster and your DBA's will start telling everyone else to code like you.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

840 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