I have this procedure in MySQL
DROP PROCEDURE IF EXISTS `AddNotificationOnPosts`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddNotificationOnPosts`(from_user INT(11),on_post_id INT(11),in_group_id INT(11))
BEGIN
DECLARE num_rows INT DEFAULT 0;
SELECT count(notification_id) FROM notifications_posts
WHERE from_user = 1;
END $$
DELIMITER ;
and this table
notification_id from_user on_post_id in_group_id date
2 1 162 3 2012-07-11 12:01:08
3 19 163 1 2012-07-11 12:03:26
4 19 164 1 2012-08-10 17:42:36
5 1 165 3 2012-08-29 12:14:01
6 1 165 3 2012-08-29 12:14:29
when i execute:
SET @p0 = '1';
SET @p1 = '2';
SET @p2 = '3';
CALL `AddNotificationOnPosts` ( @p0 , @p1 , @p2 );
it gives me:
count(notification_id)
5
why? because they are only 3 records of user_id 1
And also how do i make this procedure to:
-
Give an error or warning when values are not set
-
Get variables from the procedure, something like
SELECT count(notification_id) FROM notifications_posts WHERE from_user = @from_user;
You should use different names for arguments: