Solved

How to update null field with SQL?

Posted on 2000-04-18
3
155 Views
Last Modified: 2010-04-04
This update function works fine for non-null values, but it won't work if the field requests is null.  How do I fix it?

function IncRequests(trkid : integer) : boolean;
var ok : boolean;
     q : TQuery;
begin
  ok := false;
  q := TQuery.Create(nil);
  try
    if trkid > 0 then
      begin
        q.SQL.Clear;
        q.SQL.Add('update tracks.db set requests = requests + 1 '+
                  'where id = '+inttostr(trkid));
        q.ExecSQL;
        ok := true;
      end;
  finally
    q.Free;
  end;
  IncRequests := ok;
end;
0
Comment
Question by:rwv
[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
3 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 100 total points
ID: 2729303
There are 2 ways to solve that:

1) Before calling the routine, make sure there are no NULL values for "Requests". You can do that, by changing all nulls to zero, so your routine works:

UPDATE "tracks.db" SET Requests=0 WHERE Requests IS NULL

2) Prevent your existing routine to work on records where "Requests" has a null value. You can do that by adding an extra test to the existing WHERE clause:

  q.SQL.Add('update tracks.db set requests = requests + 1 '+
                  'where id = )'+inttostr(trkid)+') AND (Requests IS NOT NULL)');


You choose whichever way works best for your specific needs.

Yours,

Alex
0
 

Author Comment

by:rwv
ID: 2729357
Alex,
Is it possible to use an if then statement in sql?
ie -
if requests is null then requests = 1
else requests = requests + 1

Bobby
0
 
LVL 15

Expert Comment

by:simonet
ID: 2729388
Unfortunatly not, Bobby. I also wish it was possible, but so far, it's not.

Alex
0

Featured Post

Industry Leaders: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 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