?
Solved

Insert data into firebird using 'where not exists'

Posted on 2013-12-29
8
Medium Priority
?
2,570 Views
Last Modified: 2013-12-29
Hi, can anyone help me on this ?

I want to insert TWO fields using the following code: instead of

     with Algquery1 do      //zien of record aanwezig is in Teksttabel
         begin
          Close;
           SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER) '+
                      'SELECT CAST (:pTEKSTVERWERKER AS CHAR(50)) FROM RDB$DATABASE '+
                      'WHERE NOT EXISTS( SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(CAST (:pTEKSTVERWERKER AS CHAR(50)))) ';
           ParamByName('pTEKSTVERWERKER').AsString:= 'Acrobat reader';
          ExecSQL;
          Transaction.Commit;
         end;

     with Algquery1 do      //zien of record aanwezig is in Teksttabel
         begin
          Close;
           SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER, GEBRUIKER) '+
                      'SELECT CAST (:pTEKSTVERWERKER AS CHAR(50)) FROM RDB$DATABASE '+
                      'WHERE NOT EXISTS( SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(CAST (:pTEKSTVERWERKER AS CHAR(50)))) ';
           ParamByName('pTEKSTVERWERKER').AsString:= 'Acrobat reader';
          ExecSQL;
          Transaction.Commit;
         end;

Can anyone show me how to also fill in the 'GEBRUIKER' field ?

thx
Mario
0
Comment
Question by:marioway
[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
  • 5
  • 3
8 Comments
 
LVL 19

Expert Comment

by:NickUpson
ID: 39744854
is this what you want?
 SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER, GEBRUIKER) '+
                      'values (cast(:pTEKSTVERWERKER AS CHAR(50)), :GEB ')+
                      'WHERE NOT EXISTS( SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(CAST (:pTEKSTVERWERKER AS CHAR(50)))) ';
0
 

Author Comment

by:marioway
ID: 39744917
so I don't have to look in the RDB$database to see if someone else already filled in the data ?

Mario
0
 
LVL 19

Accepted Solution

by:
NickUpson earned 2000 total points
ID: 39744984
my version still does the test for existance in TEKST to see if TEKSTVERWERKER already exists there,

but you don't need to involve rdb$database

if you want to test against both fields then extend the last part to included "and upper(GEBRUIKER = upper(:geb)

geb is a variable I added to hold the value you will need to get it from somewhere
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

Author Comment

by:marioway
ID: 39744989
ok, thx Nick

kind regards
Mario
0
 

Author Comment

by:marioway
ID: 39745065
sorry Nick to bother you again, but I get an error ' WHERE'

           SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER, GEBRUIKER) '+
                      'VALUES(:pTEKSTVERWERKER, :pGEBRUIKER) '+
                      'WHERE NOT EXISTS(SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(:pTEKSTVERWERKER)) ';
           ParamByName('pTEKSTVERWERKER').AsString:= 'Acrobat reader';
           ParamByName('pGEBRUIKER').AsString:= GEBR;
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 39745095
actually, rethinking this, your original plan was ok (so long as the volumes are small)

           SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER, GEBRUIKER) '+
                      'SELECT CAST (:pTEKSTVERWERKER AS CHAR(50)) FROM RDB$DATABASE '+
                      'WHERE NOT EXISTS( SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(CAST (:pTEKSTVERWERKER AS CHAR(50)))) ';

becomes

           SQL.Text:= 'INSERT INTO TEKST (TEKSTVERWERKER, GEBRUIKER) '+
                      'SELECT CAST (:pTEKSTVERWERKER AS CHAR(50)), :gebr FROM RDB$DATABASE '+
                      'WHERE NOT EXISTS( SELECT * FROM TEKST '+
                      'WHERE UPPER(TEKSTVERWERKER) = UPPER(CAST (:pTEKSTVERWERKER AS CHAR(50)))) ';

if the tables is significant size you will need a unique index on the fields and catch the exception or use UPDATE or INSERT ... if you have firebird 2.0 or higher
1
 

Author Comment

by:marioway
ID: 39745107
ok nick I'm going to take a look at this tomorrow

thx
Mario
0
 

Author Comment

by:marioway
ID: 39745205
OK Nick, everythring is working as expected; the tables are nog big

thx a lot

regards
Mario
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

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