Solved

Insert data into firebird using 'where not exists'

Posted on 2013-12-29
8
2,431 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 500 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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

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

Read about achieving the basic levels of HRIS security in the workplace.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 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