1

I have a database on Xampp and I wish to update some tables at the same time and I wish to rollback if the value already exist in some tables.

  • I have thought:
    • trigger: but I don't like (because the server is not mine and so on)
    • BEGIN query (to check), insert (if the condition is right) COMMIT/ROLLBACK but in this way I kill the concurrency.

Is there another way or trick in order to do it?

I wish to do this:

if(value exist) rollback;
else insert;

But I have some tables so I wish to do some like that:

if(value1 in table 1 not exist && value2 in table2 not exist)
insert;
else rollback

Another example:

Table1: dog color -> Bobby black - Rex white
Table2: pot misure -> Moon 12 -  Lima 13

insert in table1 dog color  Bobby pink
insert in table2 pot misure Charly 9

rollback all because in table1 already exist Bobby

  1. In this case I can use begin transaction and rollback:
    • begin
    • transaction query in order to know if field is egual
    • rollback or insert commit

But I wish to find a way like that:

insert bla bla if(condition)

is it possible?

I have found this:

 Insert into Table1 (dog, color) SELECT 'Zulu', 'yellow' WHERE not
 exists (select * from Table1 where dog = 'Zulu')

Bu if I have some of these instructions have I use begin and commit?

Luca95
  • 109
  • 8
  • Why would you even Insert at first then Check and Rollback later ? I think you should first Check the Validation using whatever your condition on Table B then Only run Update/Insert Query on Table A. – N Subedi Aug 15 '18 at 21:20
  • Anyway, for your condition SQL have Exists operator : if exists(select 1 from table where field1 = 1111) Insert into table ... . . . Else do your other stuffs – N Subedi Aug 15 '18 at 21:23
  • Like this: https://stackoverflow.com/questions/20971680/sql-server-insert-if-not-exist ? – Luca95 Aug 15 '18 at 21:25

0 Answers0