Ho una procedura memorizzata MySQL dove trovo il valore massimo da una tabella.Come verificare se una variabile è NULL, quindi impostarla con una procedura memorizzata MySQL?
Se non c'è alcun valore, voglio impostare la variabile sulla data di ieri.
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;
Il problema è che @last_run_time
è sempre NULL.
Il seguente codice non viene eseguito per qualche motivo
IF(last_run_time IS NULL) THEN
SET last_run_time := DATE_SUB(NOW(), INTERVAL 1 DAY);
END IF;
Come posso impostare la variabile @last_run_time
correttamente?
Mi piace usare 'COALESCE' per la sostituzione di valori null. COALESCE (@last_run_time, Date_Sub (NOW(), INTERVAL 1 DAY)); http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce – actkatiemacias
Grazie. Apparentemente il problema è che non sto usando le variabili correttamente. – Jaylen