简体   繁体   中英

Using stored procedure in MySQL

I have a weather database in MySQL. My database get data from arduino, but sometimes the arduino have some error and send error value in my database. I want to make a stored procedure to reject this error. I want using if then in stored procedure. Example if temperature < 20 then MySQL reject this data. Is it possible? Help me please with the coding

this is my table

CREATE TABLE `cuaca_maritim`.`weather_data` (
 `idweather` INT(10) NOT NULL,
 `temperature` DECIMAL(4,2) NOT NULL,
 `HUMID` DECIMAL(4,2) NOT NULL,
 `AIRPRESSURE` DECIMAL(6,2) NOT NULL,
 `WIND` DECIMAL(4,2) NOT NULL,
 PRIMARY KEY (`idweather`))
  ENGINE = InnoDB
 DEFAULT CHARACTER SET = utf8
 COLLATE = utf8_bin;

Perhaps you just want a view:

create view good_weather_data as
    select wd.*
    from weather_data
    where temperature >= 20;

I don't really see why a stored procedure would be necessary. You might want a trigger that rejects invalid data values when they are loaded.

I think you can use the following stored procedure to do your work.

DELIMITER #
CREATE OR REPLACE PROCEDURE add_weather_data (IN temp INT) 
proc_main: BEGIN
IF (temp > 20) THEN
   INSERT INTO weather_data(temperature) VALUES (temp);
END IF;
END proc_main #
DELIMITER;

Here I have only consider the temp you can do the same for other parameters also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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