-2

I have a file with .sql extension in which I write a query like insertions, deletions or updates, and execute it in toadformysql . I repeat the same action many times because I have lot of queries, so it turned out that I have a lot .sql files. for the first query It works . but When I tried to add a second query in the same file and execute it , there are errors because the first query has already been executed. If the first query is delete for example, it displays an error "no such column" , which is logic beacuse I already delete the column. Is there a way that I can have a single file in which I add all my queries and while executing it , I won't have errors from old queries like duplicates or others, something like errors handling. It is because I have to keep an history of all queries. Only the query that I didn't already execute will throw an error if there is.

for example if my first query is

ALTER TABLE adbproject DROP COLUMN imageFormat

and I execute it. for the second time I want to add another query which is:

ALTER TABLE PERSON ADD MATRICULE VARCHAR(50) AFTER CODE;

So the file will to be executed will be :

ALTER TABLE adbproject DROP COLUMN imageFormat;
ALTER TABLE PERSON ADD MATRICULE VARCHAR(50) AFTER CODE;

but I have logically this error : Can't DROP 'imageFormat'; check that column/key exists. I am searching a way to avoid this error.
Thanks in advance

zinzin
  • 1
  • 2
  • How can we explain why your second query didn't work, when you did not show us what you're doing? – Ken White Apr 06 '16 at 17:09
  • Possible duplicate of [Execute mysql queries using the same file several times](http://stackoverflow.com/questions/36457046/execute-mysql-queries-using-the-same-file-several-times) – Alon Eitan Apr 06 '16 at 17:09
  • Please [edit] your question and put the additional details there, instead of burying them in comments. – Ken White Apr 06 '16 at 17:23
  • @KenWhite edit done.. any help ?? – zinzin Apr 06 '16 at 17:34

1 Answers1

1

Two options:

  1. Write all the commands to the file and execute the whole file only once.
  2. After execution of each command, delete content of the file.
abrcek
  • 93
  • 5
  • It is actually what i am doing until now.every command has its own file,because i have to keep history of all commands done during the project. I was wondering if there is a way to do it in one file executable many times with errors handling. – zinzin Apr 06 '16 at 20:31
  • In that case, proceed as I wrote in option 1. Put all commands into single file and then execute whole file only once. Do not execute every single command, just the whole file. – abrcek Apr 06 '16 at 21:02