Solved

Insert data into firebird using 'where not exists'

Posted on 2013-12-29
8
2,026 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
  • 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
 

Author Comment

by:marioway
ID: 39744989
ok, thx Nick

kind regards
Mario
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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
0
 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now