Preface:
I have a MS Access 2010 front end running against a MySQL back end.
I have companies that have sites and employees that are linked to those company sites and want to allow the users to delete the sites if required BUT this would only be possible if there where no employees linked to those sites which is fine as the MySQL foreign keys enforce this anyway so if I try to do a delete on a site and there are still employees associated with that site it errors.
BUT the only way I have to test to see if the delete is valid (will work) is to try and delete it BUT then it will do the delete IF it is OK. But I want to be able to test if the delete is possible before doing the delete so I can work out if all the other code that also occurs just before a delete should run.
I thought about using “Start Transaction; ….. Rollback;” but that can’t be done from access to MySQL in any way I can come up with. I have tried:
1) Passthrough Query (but apparently you can only pass one query per operation so I can pass the delete but not the “Start Transaction;” or Rollback;”) 🙁
2) Stored Procedure – Passing the delete into a stored procedure that executes it between the “Start Transaction;” and “RollBack;” like:
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `SP_TestStatementExecution`(
IN SQLString TEXT
)
BEGIN
START TRANSACTION;
SET @query := SQLString;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ROLLBACK;
END
BUT that doesn’t seem to error when it’s invalid 🙁
I would like to be able to pass in several statements in one go really so was thinking of something like this:
------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `SP_TestStatementsExecution`(
IN SQLString TEXT
)
BEGIN
SET @Delim = ";";
SET @Pos2 = INSTR(SUBSTRING(SQLString,@Pos1), @Delim);
START TRANSACTION;
WHILE @Pos2 > 0 DO
SET @query = SUBSTRING(
SQLString,
1,
@Pos2
);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET SQLString = SUBSTRING(SQLString,@Pos2 + 1);
SET @Pos2 = INSTR(SUBSTRING(SQLString,1), @Delim);
END WHILE;
ROLLBACK;
END
BUT until I get an error to STOP the execution and return an error to access that is no good…
Question:
How can I test if a series of SQL statements will execute in MySQL without committing to the DB?
I am currently working on using Access’ Rollback with workbooks directly but would prefer to be able to do it in MySQL directly if possible
The solution I went with was to use Accesses own Workspace to roll back the transaction. It wasn’t really ideal but has done the job I needed.
The method was:
Hope this helps someone else. 🙂