CREATE TRIGGER input_verification AFTER INSERT ON InputHistory
FOR EACH ROW
WHEN NO EXISTS (SELECT * FROM InputHistory JOIN Intput on InputHistory.inputId = Input.inputId WHERE InputHistory.inputId = NEW.inputId AND InputHistory.inputHistoryOrderNumber=Input.inputOrderNumber AND InputHistory.inputHistoryStatus >= 300)
BEGIN
DECLARE configId INTEGER;
SELECT Input.inputConfigurationId INTO configId FROM Input JOIN InputHistory ON Input.inputId = InputHistory.inputId WHERE NEW.inputId=Input.inputId;
UPDATE Configuration SET configurationReady = true WHERE configurationId= configId;
END
Related
I'm in a problem with the following query ..
After inserting in the table rtoxvta record by record, I need before the end of the loop eliminate the record of the vta table prevously selected.
For that i declare a new variable and assign it the vta id .. but where do I do it?
DROP PROCEDURE IF EXISTS ROWPERROW;
DELIMITER ;;
CREATE PROCEDURE ROWPERROW()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE indexx INT DEFAULT 0;
#DECLARE aux INT DEFAULT 0;
SELECT COUNT(*) FROM rto INTO n;
SET n = n +1;
SET indexx=0;
WHILE indexx<n DO
INSERT INTO rtoxvta (fecharto, importerto, fechavta, importevta)
SELECT r.fecha, r.importe, v.fecha, v.importe
FROM vta v
left outer join rto r
ON r.id = indexx
WHERE v.fecha >= r.fecha
AND (
(v.importe = r.importe) OR
(v.importe + 2 = r.importe) OR
(v.importe + 1 = r.importe) OR
(v.importe - 1 = r.importe) OR
(v.importe - 2 = r.importe))
LIMIT 1;
SET indexx = indexx + 1;
#DELETE FROM vta WHERE id = aux;
END WHILE;
End;
;;
CALL ROWPERROW();
You'll have to do it as two steps. First get the ID of the record you're going to insert into a variable, then insert that record. Then you can delete the record.
WHILE indexx<n DO
SELECT v.id, r.id INTO #v_id, #r_id
FROM vta v
join rto r
ON r.id = indexx
WHERE v.fecha >= r.fecha
AND r.importe BETWEEN v.importe - 2 AND v.importe + 2
LIMIT 1;
INSERT INTO rtoxvta (fecharto, importerto, fechavta, importevta)
SELECT r.fecha, r.importe, v.fecha, v.importe
FROM vta v
CROSS JOIN rto r
WHERE v.id = #v_id AND r.id = #r_id;
SET indexx = indexx + 1;
DELETE FROM vta WHERE id = #v_id;
END WHILE;
Also, if you're using a column from rto in the WHERE clause, you should use INNER JOIN rather than LEFT OUTER JOIN. The WHERE condition will not match any rows that don't have a match in rto, so there's no point in using an outer join.
I have this trigger
DELIMITER ;
DELIMITER $$
CREATE TRIGGER when_auction_ends
AFTER UPDATE ON Auctions
FOR EACH ROW
BEGIN
SELECT A.vin, A.status, A.winner, A.minprice, B.amount, B.buyer INTO #vin, #stat, #winner, #minprice, #amount, #buyer
FROM Auctions A, Bids B WHERE A.status = 'Inactive' AND A.winner = null AND A.vin = B.vin AND B.amount = (SELECT MAX(amount) FROM Bids B WHERE B.vin = A.vin AS
IF #amount < #minprice THEN
UPDATE Auctions
SET A.winner = 'NONE'
WHERE A.vin = B.vin;
ELSEIF #amount >= #minprice THEN
UPDATE Auctions
SET A.winner = B.buyer
WHERE A.vin = B.vin;
END IF ;
END$$
DELIMITER ;
But when it activates, the values are not updated. I am thinking that its because mysql does not know what A.winner is in "SET A.winner". I tried looking online on how to use OLD and NEW but did not have any luck.
I have three tables ( restaurants - votes - comments) . I want to create a trigger which let me update two fields in restaurants ( restaurant_rate_average , restaurant_comment_count ) . Here is my trigger :
DELIMITER $$
CREATE
TRIGGER `change comment status` AFTER UPDATE ON `comments`
FOR EACH ROW
BEGIN
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_comment_count = SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
UPDATE restaurants
INNER JOIN votes ON restaurants.restaurant_id = votes.vote_foreign_key
SET restaurants.restaurant_rate_average = SELECT (AVG(votes.vote_num))
WHERE votes.vote_model = 'restaurant'
AND WHERE old.comment_status<>NEW.comment_status;
END;
$$
DELIMITER ;
Actually I have an error :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (COUNT(votes.vote_id))
WHERE votes.vote_model = 'restaurant'
' at line 8
I've tried to make complexion less and replace SELECT (COUNT(votes.vote_id)) with 2 and I get new error :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE comments.comment_status<>NEW.comment_status;
UPDATE resta' at line 9
How to resolve it?
EDITED :
here is my pseudo tables :
restaurants:
restaurant_id, restaurant_comment_count(int) , restaurant_rate_average(int)
comments:
comment_id , comment_status(boolean) , comment_content(varchar) , votes_vote_id(id)
votes:
vote_id , vote_foreign_key(id), vote_model(varchar)
EDITED 2 :
I create new simple trigger which just join three tables and it works like charm:
BEGIN
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET res.restaurant_rate_average = 2
WHERE cm.comment_status = NEW.comment_status;
END
But if I replace SET res.restaurant_rate_average = 2 with SELECT AVG(v.vote_id) I got an error. I also edit last trigger to :
BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num)
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
END
I got an error in line 4. All I want is replace static number with SELECT AVG(votes.vote_num)
DROP TRIGGER IF EXISTS `update_comment_status`;CREATE DEFINER=`root`#`localhost` TRIGGER `update_comment_status` AFTER UPDATE ON `comments` FOR EACH ROW BEGIN
DECLARE RESTAURANT_RATE_AVERAGE INTEGER;
DECLARE RESTAURANT_COMMENT_COUNT INTEGER;
SET RESTAURANT_RATE_AVERAGE := (SELECT AVG(votes.vote_num) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_rate_average = RESTAURANT_RATE_AVERAGE
WHERE cm.comment_status = NEW.comment_status;
SET RESTAURANT_COMMENT_COUNT := (SELECT COUNT(votes.vote_id) FROM votes);
UPDATE restaurants res
INNER JOIN votes v ON v.vote_foreign_key = res.restaurant_id AND v.vote_foreign_key = 'restaurant'
INNER JOIN comments cm ON cm.votes_vote_id = v.vote_id
SET restaurant_comment_count = RESTAURANT_COMMENT_COUNT
WHERE cm.comment_status = NEW.comment_status;
END
Running into an error when trying to crate a stored procedure in MySQL (5.1).
Whenever I try and run this SQL script, I am presented with:
Error 1064 (42000) At line 3: You have an error in your SQL syntax.
Near 'DECLARE checkExists INT; SET checkExists = 0; SELECT count(*) INTO c' at line 29
From what I understand, declaring a variable this way should be acceptable.
I have included the SQL script below.
DELIMITER //
CREATE PROCEDURE add_movie(
IN movieTitle VARCHAR(100),
IN movieYear INT,
IN movieDirector VARCHAR(100),
IN movieBannerURL VARCHAR(100),
IN movieTrailerURL VARCHAR(100),
IN starFirstName VARCHAR(50),
IN starLastName VARCHAR(50),
IN starDateOfBirth DATE,
IN starPhotoURL VARCHAR(200),
IN genreName VARCHAR(32),
OUT movieAdded INT,
OUT starAdded INT,
OUT genreAdded INT,
OUT movieStarLinkAdded INT,
OUT movieGenreLinkAdded INT)
LANGUAGE SQL
NOT DETERMINISTIC
SQL SECURITY INVOKER
COMMENT 'Adds Movie, Star, Genre and respective links to DB if they do not exist'
BEGIN
SET movieAdded = 0;
SET starAdded = 0;
SET genreAdded = 0;
SET movieStarLinkAdded = 0;
SET movieGenreLinkAdded = 0;
DECLARE checkExists INT;
SET checkExists = 0;
SELECT count(*) INTO checkExists FROM movies m WHERE m.title = movieTitle;
IF(checkExists > 0) THEN
INSERT INTO movies(title, year, director, banner_url, trailer_url)
VALUES (movieTitle, movieYear, movieDirector, movieBannerURL, movieTrailerURL);
SET movieAdded = 1;
END IF;
SET checkExists = 0;
SELECT count(*) INTO checkExists FROM stars s WHERE s.first_name = starFirstName AND s.last_name = starLastName;
IF(checkExists > 0) THEN
INSERT INTO stars(first_name, last_name, dob, photo_url)
VALUES (starFirstName, starLastName, starDateOfBirth, starPhotoURL);
SET starAdded = 1;
END IF;
SET checkExists = 0;
SELECT count(*) INTO checkExists FROM genres g WHERE g.name = genreName;
IF(checkExists > 0) THEN
INSERT INTO genres(name)
VALUES (genreName);
SET genreAdded = 1;
END IF;
SET checkExists = 0;
SELECT count(*) INTO checkExists FROM stars_in_movies sm, stars s, movies m
WHERE m.title = movieTitle AND s.first_name = starFirstName
AND s.last_name = starLastName AND sm.star_id = s.id AND sm.movie_id = m.id;
IF(checkExists > 0) THEN
INSERT INTO stars_in_movies(star_id, movie_id)
VALUES(
SELECT s.id, m.id FROM stars s, movies m WHERE s.first_name = starFirstName
AND s.last_name = starLastName AND m.title = movieTitle);
SET movieStarLinkAdded = 1;
END IF;
SET checkExists = 0;
SELECT count(*) INTO checkExists FROM genres_in_movies gm, genres g, movies m
WHERE m.title = movieTitle AND genre.name = genreName
AND gm.movie_id = m.id AND gm.genre_id = g.id;
IF(checkExists > 0) THEN
INSERT INTO genres_in_movies(genre_id, movie_id)
VALUES(
SELECT g.id, m.id FROM genres g, movies m
WHERE g.name = genreName AND m.title = movieTitle);
SET movieGenreLinkAdded = 1;
END IF;
END //
DELIMITER ;
If someone could help me understand what I am doing incorrectly here, I would greatly appreciate it. Thank you.
Move all DECLARE statements to the start of a BEGIN .. END block, next to BEGIN.
See documentation: https://dev.mysql.com/doc/refman/5.6/en/declare.html
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
I have created a following stored procedure to insert rows in the table panel_user:
CREATE PROCEDURE panel_users()
BEGIN
DECLARE i INTEGER;
DECLARE user_id BIGINT(20);
DECLARE panel_id BIGINT(20);
DECLARE wt DECIMAL(10,6);
DECLARE level DECIMAL(6,2);
DECLARE total INTEGER;
SET i=1;
SELECT COUNT(*) INTO total FROM smartmeter_usage.users_reporting;
WHILE i<=total DO
SELECT u.userId INTO user_id, p.panelId INTO panel_id, ur.usage_percent INTO level, ipr.wt_base INTO wt
FROM mondrainusage.user_d u
INNER JOIN smartmeter_usage.imei_phone_reporting ipr ON u.imeiNumber = ipr.IMEI
INNER JOIN smartmeter_usage.users_reporting ur ON ur.mobile_number = ipr.mobile_number
INNER JOIN smartmeter_usage.panel pn ON pn.panel_id = ur.panel_id
INNER JOIN mondrainusage.panel p ON p.panelName = pn.panel_name
WHERE u.userId = i;
INSERT INTO mondrainusage.panel_user (panelId, userId, userWeight, usageLevel) VALUES(panel_id, user_id, wt, level);
SET i = i+1;
END WHILE
END
I am getting following error:
Undeclared variable: p
I am not getting what is wrong here in my query. Is there a way out of it?
EDIT:
k I got the solution for the above problem. here is the solution:
CREATE PROCEDURE panel_users()
BEGIN
DECLARE i INTEGER;
DECLARE user_id BIGINT(20);
DECLARE panel_id BIGINT(20);
DECLARE wt DECIMAL(10,6);
DECLARE level DECIMAL(6,2);
DECLARE total INTEGER;
SET i=1;
SELECT COUNT(*) INTO total FROM smartmeter_usage.users_reporting;
WHILE i<=total DO
SELECT u.userId, p.panelId, ur.usage_percent, ipr.wt_base INTO user_id, panel_id, level, wt
FROM mondrainusage.user_d u
INNER JOIN smartmeter_usage.imei_phone_reporting ipr ON u.imeiNumber = ipr.IMEI
INNER JOIN smartmeter_usage.users_reporting ur ON ur.mobile_number = ipr.mobile_number
INNER JOIN smartmeter_usage.panel pn ON pn.panel_id = ur.panel_id
INNER JOIN mondrainusage.panel p ON p.panelName = pn.panel_name
WHERE u.userId = i;
INSERT INTO mondrainusage.panel_user (panelId, userId, userWeight, usageLevel) VALUES(panel_id, user_id, wt, level);
SET i = i+1;
END WHILE
END
Bit now it is giving me syntax error near 'END' at line 21.
Why is this so?
EDIT:
Got the solution:
END WHILE;
Here is a solution that worked.
CREATE PROCEDURE panel_users()
BEGIN
DECLARE i INTEGER;
DECLARE user_id BIGINT(20);
DECLARE panel_id BIGINT(20);
DECLARE wt DECIMAL(10,6);
DECLARE level DECIMAL(6,2);
DECLARE total INTEGER;
SET i=1;
SELECT COUNT(*) INTO total FROM smartmeter_usage.users_reporting;
WHILE i<=total DO
SELECT u.userId, p.panelId, ur.usage_percent, ipr.wt_base INTO user_id, panel_id, level, wt
FROM mondrainusage.user_d u
INNER JOIN smartmeter_usage.imei_phone_reporting ipr ON u.imeiNumber = ipr.IMEI
INNER JOIN smartmeter_usage.users_reporting ur ON ur.mobile_number = ipr.mobile_number
INNER JOIN smartmeter_usage.panel pn ON pn.panel_id = ur.panel_id
INNER JOIN mondrainusage.panel p ON p.panelName = pn.panel_name
WHERE u.userId = i;
INSERT INTO mondrainusage.panel_user (panelId, userId, userWeight, usageLevel) VALUES(panel_id, user_id, wt, level);
SET i = i+1;
END WHILE;
END