MySQL存储过程:如果表存在,则删除条目

时间:2018-06-07 07:51:47

标签: mysql stored-procedures psql

我创建了一个存储过程,它应该从某些表中删除数据。

在我的高效数据库中,我有表格,我没有在我的本地数据库中(不同的模式名称)。

是否有一种优雅的方式来查询数据表是否存在以避免异常?也许声明一个捕获特定错误的错误处理程序,并且应该继续。

CREATE PROCEDURE `deletion`(IN s_id varchar(255))
BEGIN   
    DECLARE has_error BOOL DEFAULT FALSE;
    /* for all other exception there should be a rollback, if table does not exist
        then continue*/
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET has_error = TRUE; 

START TRANSACTION;

DELETE FROM a WHERE id = s_id;
DELETE FROM b WHERE id = s_id;
DELETE FROM c WHERE id = s_id;
DELETE FROM d WHERE id = s_id;

IF has_error THEN
  ROLLBACK;
 ELSE
  COMMIT;
END IF;

END

更新

我尝试了这个,但这不起作用。即使表存在,也不会删除该条目:

 IF EXISTS (SELECT 1 from information_schema.tables 
    WHERE table_schema = 'a' ) then
        DELETE FROM at WHERE id = s_id; 
    END IF;

提前感谢!

0 个答案:

没有答案