1. <legend id='hW7Qe'><style id='hW7Qe'><dir id='hW7Qe'><q id='hW7Qe'></q></dir></style></legend>
  2. <small id='hW7Qe'></small><noframes id='hW7Qe'>

    <tfoot id='hW7Qe'></tfoot>

    1. <i id='hW7Qe'><tr id='hW7Qe'><dt id='hW7Qe'><q id='hW7Qe'><span id='hW7Qe'><b id='hW7Qe'><form id='hW7Qe'><ins id='hW7Qe'></ins><ul id='hW7Qe'></ul><sub id='hW7Qe'></sub></form><legend id='hW7Qe'></legend><bdo id='hW7Qe'><pre id='hW7Qe'><center id='hW7Qe'></center></pre></bdo></b><th id='hW7Qe'></th></span></q></dt></tr></i><div id='hW7Qe'><tfoot id='hW7Qe'></tfoot><dl id='hW7Qe'><fieldset id='hW7Qe'></fieldset></dl></div>
        <bdo id='hW7Qe'></bdo><ul id='hW7Qe'></ul>
    2. 如何检查变量是否为 NULL,然后使用 MySQL 存储过程设置它?

      How to check if a variable is NULL, then set it with a MySQL stored procedure?(如何检查变量是否为 NULL,然后使用 MySQL 存储过程设置它?)

          <tbody id='ZoUVb'></tbody>

          <legend id='ZoUVb'><style id='ZoUVb'><dir id='ZoUVb'><q id='ZoUVb'></q></dir></style></legend>
          <tfoot id='ZoUVb'></tfoot>
            <bdo id='ZoUVb'></bdo><ul id='ZoUVb'></ul>

            <small id='ZoUVb'></small><noframes id='ZoUVb'>

          • <i id='ZoUVb'><tr id='ZoUVb'><dt id='ZoUVb'><q id='ZoUVb'><span id='ZoUVb'><b id='ZoUVb'><form id='ZoUVb'><ins id='ZoUVb'></ins><ul id='ZoUVb'></ul><sub id='ZoUVb'></sub></form><legend id='ZoUVb'></legend><bdo id='ZoUVb'><pre id='ZoUVb'><center id='ZoUVb'></center></pre></bdo></b><th id='ZoUVb'></th></span></q></dt></tr></i><div id='ZoUVb'><tfoot id='ZoUVb'></tfoot><dl id='ZoUVb'><fieldset id='ZoUVb'></fieldset></dl></div>
              • 本文介绍了如何检查变量是否为 NULL,然后使用 MySQL 存储过程设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 MySQL 存储过程,我可以在其中找到表中的最大值.

                I have a MySQL stored procedure where I find the max value from a table.

                如果没有值,我想将变量设置为昨天的日期.

                If there is no value I want to set the variable to yesterday's date.

                DECLARE current_procedure_name CHAR(60) DEFAULT 'accounts_general';
                DECLARE last_run_time datetime DEFAULT NULL;
                DECLARE current_run_time datetime DEFAULT NOW();
                
                -- Define the last run time
                SET last_run_time := (SELECT MAX(runtime) 
                FROM dynamo.runtimes WHERE procedure_name = @current_procedure_name);
                
                -- if there is no last run time found then use yesterday as starting point
                IF(@last_run_time IS NULL) THEN
                    SET last_run_time := DATE_SUB( NOW(), INTERVAL 1 DAY);
                END IF;
                
                SELECT @last_run_time;
                

                问题在于 @last_run_time 始终为 NULL.

                The problem is that @last_run_time is always NULL.

                以下代码由于某种原因没有被执行

                The following code is not being executed for some reason

                IF(last_run_time IS NULL) THEN
                    SET last_run_time := DATE_SUB( NOW(), INTERVAL 1 DAY);
                END IF;
                

                如何正确设置变量 @last_run_time?

                推荐答案

                @last_run_time 是一个 9.4.用户定义的变量和last_run_time datetime一个13.6.4.1.局部变量 DECLARE 语法,是不同的变量.

                @last_run_time is a 9.4. User-Defined Variables and last_run_time datetime one 13.6.4.1. Local Variable DECLARE Syntax, are different variables.

                尝试:SELECT last_run_time;

                更新

                示例:

                /* CODE FOR DEMONSTRATION PURPOSES */
                DELIMITER $$
                
                CREATE PROCEDURE `sp_test`()
                BEGIN
                    DECLARE current_procedure_name CHAR(60) DEFAULT 'accounts_general';
                    DECLARE last_run_time DATETIME DEFAULT NULL;
                    DECLARE current_run_time DATETIME DEFAULT NOW();
                
                    -- Define the last run time
                    SET last_run_time := (SELECT MAX(runtime) FROM dynamo.runtimes WHERE procedure_name = current_procedure_name);
                
                    -- if there is no last run time found then use yesterday as starting point
                    IF(last_run_time IS NULL) THEN
                        SET last_run_time := DATE_SUB(NOW(), INTERVAL 1 DAY);
                    END IF;
                
                    SELECT last_run_time;
                
                    -- Insert variables in table2
                    INSERT INTO table2 (col0, col1, col2) VALUES (current_procedure_name, last_run_time, current_run_time);
                END$$
                
                DELIMITER ;
                

                这篇关于如何检查变量是否为 NULL,然后使用 MySQL 存储过程设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Accessing another user#39;s table within an Oracle Stored Procedure(在 Oracle 存储过程中访问另一个用户的表)
                How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?(如何使用 MyBatis 将 Java 对象列表传递给 Oracle 存储过程?)
                Set the variable result, from query(设置变量结果,来自查询)
                What is dynamic SQL?(什么是动态 SQL?)
                Mysql - How to quit/exit from stored procedure(Mysql - 如何退出/退出存储过程)
                <i id='Ka2dW'><tr id='Ka2dW'><dt id='Ka2dW'><q id='Ka2dW'><span id='Ka2dW'><b id='Ka2dW'><form id='Ka2dW'><ins id='Ka2dW'></ins><ul id='Ka2dW'></ul><sub id='Ka2dW'></sub></form><legend id='Ka2dW'></legend><bdo id='Ka2dW'><pre id='Ka2dW'><center id='Ka2dW'></center></pre></bdo></b><th id='Ka2dW'></th></span></q></dt></tr></i><div id='Ka2dW'><tfoot id='Ka2dW'></tfoot><dl id='Ka2dW'><fieldset id='Ka2dW'></fieldset></dl></div>
                    <bdo id='Ka2dW'></bdo><ul id='Ka2dW'></ul>
                        <tbody id='Ka2dW'></tbody>
                    • <small id='Ka2dW'></small><noframes id='Ka2dW'>

                        <legend id='Ka2dW'><style id='Ka2dW'><dir id='Ka2dW'><q id='Ka2dW'></q></dir></style></legend><tfoot id='Ka2dW'></tfoot>