Re: empty values in INSERT INTO statement
- From: "TB" <tbpostbox-googlegroups@xxxxxxxxx>
- Date: Sat, 23 Apr 2005 08:51:49 +0200
Of course I want to hear more about parameters, if you don't mind.
And yes, SQL is one the more commonly words spoken by me. I find it a very
interesting dicipline which properly harnessed with other (for me) pending
issues like Getrows() gives fantastic results.
TB
"Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx> wrote in message
news:OvNsup5RFHA.3088@xxxxxxxxxxxxxxxxxxxxxxx
> TB wrote:
>> I have this ASP page that inserts new records into an Access database
>> via an execute statement.
>>
>> Taking the data from a form, I build my SQL statement like this:
>> <%
>> strSQL = "INSERT INTO mytable (Firstname, Lastname, Department,
>> Extension) _
>> & " VALUES ('" & CStr(Replace(Request.Form("Firstname"), "'",
>> "''")) _ & "', '" & CStr(Replace(Request.Form("Lastname"), "'",
>> "''")) _ & "', " & CInt(Replace(Request.Form("Department"), "'",
>> "''")) _ & ", " & CInt(Replace(Request.Form("Extension"), "'",
>> "''")) & ")" %>
>>
>> That works ok as long as ALL the fields in the form contain data. If
>> one of the number fields (Department and Extension) are empty, and
>> error is returned.
>>
>> I guess it has something to do with the fact that text values are
>> surrounded by delimiting apostrophies while number fields are not.
>>
>> So with complete data the above lines generate the following SQL
>> statement:
>> INSERT INTO mytable (Firstname, Lastname, Department, Extension)
>> VALUES ('John', 'Doe', 3, 55)
>>
>> If the texts fields are empty then the statement would be:
>>
>> INSERT INTO mytable (Firstname, Lastname, Department, Extension)
>> VALUES ('', '', 3, 55)
>>
>> which is also OK.
>>
>> But if the number fields are emtpy the statement become:
>>
>> INSERT INTO mytable (Firstname, Lastname, Department, Extension)
>> VALUES ('John', 'Doe', , )
>>
>> and that produces an error.
>>
>> How can I change the above SQL building code so as to avoid this kind
>> of problem? For example if I could make the word NULL appear instead
>> of (nothing) then SQL statement would work
>>
>> Any suggestions would be highly appreciated.
>>
>> Thanks
>>
> As usual, your problem stems from using dynamic sql (do I sound like a
> broken record rere? <grin>)
>
>
> If you want to stick with dynamic sql, you will need to substitute the
> word "null" for your request variables when they are empty. That way, the
> resulting sql looks like this:
>
> ... VALUES ('John', 'Doe', null, null )
>
>
> With parameters, the problem is simpler. Post back if you wish to hear
> more.
>
> Bob Barrows
>
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
.
- Follow-Ups:
- Re: empty values in INSERT INTO statement
- From: Bob Barrows [MVP]
- Re: empty values in INSERT INTO statement
- References:
- empty values in INSERT INTO statement
- From: TB
- Re: empty values in INSERT INTO statement
- From: Bob Barrows [MVP]
- empty values in INSERT INTO statement
- Prev by Date: Re: empty values in INSERT INTO statement
- Next by Date: Re: empty values in INSERT INTO statement
- Previous by thread: Re: empty values in INSERT INTO statement
- Next by thread: Re: empty values in INSERT INTO statement
- Index(es):
Relevant Pages
|