?
Solved

How to update null field with SQL?

Posted on 2000-04-18
3
Medium Priority
?
157 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 300 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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