Solved

How to update null field with SQL?

Posted on 2000-04-18
3
148 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
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Unfortunatly not, Bobby. I also wish it was possible, but so far, it's not.

Alex
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now