Solved

Insert data into firebird using 'where not exists'

Posted on 2013-12-29
8
2,226 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
business objects to connect to MSSQL 6 84
Question about consuming GB from Comcast 5 61
T-SQL: Do I need CLUSTERED here? 13 59
Mysql Left Join Case 10 70
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

809 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