简体   繁体   English

MySQL 中的断言

[英]Assertion in MySQL

I have a SQL script to run against a large database.我有一个针对大型数据库运行的 SQL 脚本。 I'd like to put a couple of simple queries at the start, just as a sanity check.我想在开始时提出几个简单的查询,作为健全性检查。

Is there any way to write an assertion in MySQL?有没有办法在 MySQL 中编写断言? Or any kind of "select ..., and if it doesn't match this value, then abort the entire script"?或者任何类型的“选择...,如果它与这个值不匹配,则中止整个脚本”?

Some crazy code.一些疯狂的代码。 Main point is: SET could raise error for mysql variables.要点是: SET可能会引发 mysql 变量的错误。

For example.例如。

 SET @value = 0;
 SET SESSION sql_mode = if(@value, @@SESSION.sql_mode, 'something wrong uphere'); 

Would output ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'something wrong uphere' and execution would be stopped.将输出ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'something wrong uphere'并且执行将停止。

This approach is not semantic but it works.这种方法不是语义的,但它有效。

You could put the entire script in an if statement, depending on what kind of value you need to check, here's an example:您可以将整个脚本放在 if 语句中,具体取决于您需要检查的值类型,这是一个示例:

DECLARE @value int
SET @value = (SELECT COUNT(*) FROM dbo)

IF @value >0
BEGIN
 --Do Stuff Here
END

You can also do this via a stored procedure / function, as in the example below:您也可以通过存储过程/函数执行此操作,如下例所示:

CREATE FUNCTION `RunProcess`() RETURNS INT(11)
runProcess:BEGIN

DECLARE check_value INT;
DECLARE error_code INT;

SELECT COUNT(*) FROM dbo INTO check_value;

IF check_value = 0 THEN set error_code = 666;
    LEAVE runProcess;
    RETURN error_code;
END IF;

...
...
END;

One way to go about this could be to make an insert fail on purpose if you assertion fails.解决此问题的一种方法可能是在断言失败时故意使插入失败。 This is not pretty, but it is simple and it works.这并不漂亮,但它很简单并且有效。

Let's say Table1 contains columnA and columnB.假设 Table1 包含 columnA 和 columnB。 You want to insert into this table the values ('any', 'thing') , but ONLY if these values fulfil some conditions.您想在该表中插入值('any', 'thing') ,但前提是这些值满足某些条件。

Then do something like this:然后做这样的事情:

SELECT 'any', 'thing' INTO @setA, @setB FROM DUAL WHERE ( your conditions...);
INSERT INTO Table1 (columnA, columnB) VALUES (@setA, @setB);

the 'any' and 'thing' values would obviously be inserted at runtime in the query. 'any' 和 'thing' 值显然会在运行时插入查询中。 If you Table1 is set up to accept only non-null values in column A or B, this will fail when your conditions fail.如果您将 Table1 设置为仅接受 A 列或 B 列中的非空值,则当您的条件失败时,这将失败。

A very simple nonsensical example (this obviously can be achieved otherwise, this is only an example :-) ), if you want the values to differ:一个非常简单的无意义示例(这显然可以通过其他方式实现,这只是一个示例 :-) ),如果您希望值不同:

SELECT 'any', 'thing' INTO @setA, @setB FROM DUAL WHERE ( 'a'!='b');
INSERT INTO Table1 (columnA, columnB) VALUES (@setA, @setB);

The test condition doesn't have to be on the values you are trying to insert:测试条件不必在您尝试插入的值上:

SELECT 'any', 'thing' INTO @setA, @setB FROM DUAL WHERE ( 'another' IS NOT NULL);
INSERT INTO Table1 (columnA, columnB) VALUES (@setA, @setB);

If your goal is not to do perform an insert, the same logic can be applied on different transaction types, the idea being to alter the variables so they make the query fail.如果您的目标不是执行插入,则可以将相同的逻辑应用于不同的事务类型,其想法是更改变量以使查询失败。

Or you can create a dummy table just for this purpose, with a non-null column, and always insert your assertion result in the dummy table's first row.或者您可以为此目的创建一个带有非空列的虚拟表,并始终将您的断言结果插入虚拟表的第一行。 Not pretty, but it works :-)不漂亮,但它有效:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM