DELIMITER $$
CREATE PROCEDURE abc(IN _uid VARCHAR(15))
BEGIN
SELECT COUNT(filename)
FROM file
WHERE userid = _uid
UNION ALL
SELECT COUNT(file)
FROM fileupload
WHERE userid = _uid
END $$
DELIMITER ;
As this my query , As I need two select statement output
but it getting 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 'END' at line 10
Try:
mysql> DELIMITER $$
mysql> CREATE PROCEDURE abc(IN _uid VARCHAR(15))
-> BEGIN
-> SELECT COUNT(filename)
-> FROM file
-> WHERE userid = _uid
-> UNION ALL
-> SELECT COUNT(file)
-> FROM fileupload
-> -- WHERE userid = _uid
-> WHERE userid = _uid;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
Related
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): 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 UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;
The table was defined as follow:
I try to declare a variables in using 'declare',but it always ruturn errors and I don't know why.
the codeļ¼
mysql> DELEMITER //
mysql> CREATE PROCEDURE emp_age_count(IN age1 int,IN age2 int,OUT count INT)
-> BEGIN
-> DECLARE temp FLOAT;
-> DECLARE emp_age CURSOR FOR SELECT empage FROM emp;
-> DECLARE EXIT HANDLER FOR NOT FOUND
-> CLOSE emp_age;
-> SET #sum=0;
-> SELECT COUNT(*) INTO count FROM emp
-> WHERE empage>age1 AND empage<age2;
-> OPEN emp_age;
-> REPEAT
-> FETCH emp_age INTO temp;
-> IF temp>age1 AND temp<age2;
-> THEN SET #sum=#sum+temp;
-> END IF;
-> UNTIL 0 END REPEAT;
-> CLOSE emp_age;
-> END//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ';
THEN SET #sum=#sum+temp;
END IF;
UNTIL 0 END REPEAT;
CLOSE emp_age;
END' at line 13
Please remove the ";" at the end of the 'IF' condition
It should be
IF (temp>age1 AND temp<age2)
THEN
SET #sum=#sum+temp;
END IF;
I want to modify a working trigger that looks like this:
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
INSERT INTO j (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
);
to execute only on the success of an IF statement. I can't get the right syntax for this modification.
I'm trying to achieve the following:
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
IF (1=1) THEN
INSERT INTO d (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
); /* <--- LINE 21 */
END IF;
Result: ERROR 1064: Check for right syntax near '' at line 21
Things I've tried:
wrapping the IF ... END IF; in BEGIN .. END;
switching delimiter to $$
I'm using MySQL 5.0.95 for RHEL.
Any help is greatly appreciated.
Try this
delimiter //
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
BEGIN
IF 1=1 THEN
INSERT INTO d (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
);
END IF;
END; //
delimiter ;
Here is one I just tried on my mysql but its for update almost same with your logic
mysql> delimiter //
mysql> create trigger tbl_two_ins
-> after insert on tabletwo
-> for each row
-> begin
-> if 1=1 then
-> update tableone t1 set t1.tb2_id = new.tab2_id where t1.tb1_id = new.tb1_id ;
-> end if ;
-> end ; //
Query OK, 0 rows affected (0.56 sec)
mysql> delimiter ;
CREATE PROCEDURE `go`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN DECLARE d_z CONDITION FOR SQLSTATE '35241';
SELECT COUNT(*)as #a from _time
IF #a>0 THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END;
error:SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#a from _time
IF #a>0 THEN SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrr' at line 9
The problem is with the select statement "#a" is alias name which cant be used further queries.
You can use peterm query "into" instead of as
Hope this helps
Happy Coding
You has some syntax issues. Following works for me:
DELIMITER //
CREATE PROCEDURE `go`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE d_z CONDITION FOR SQLSTATE '35241';
select count(*) INTO #a from _time;
IF #a>0 THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END
//
Important is to:
Use into #variable syntax for selecting a value into a var
Use DELIMITER for termination of multiline procedure body
Your immediate error is caused by the malformed SELECT statement. You have to a proper SELECT INTO syntax
Change
SELECT COUNT(*)as #a from _time
to
SELECT COUNT(*) INTO #a FROM _time
^^^^
Now there are several other issues with your code:
There is no need to use a user(session) variable, you could've used local variable instead
You don't want to get total number of all rows just to tell whether you have rows or not in your table. If you do you'll pay performance penalty (if of course you're not using MyISAM). You can leverage NOT EXISTS() for that or LIMIT 1 clause.
That being said a streamlined version of your procedure might look like
DELIMITER $$
CREATE PROCEDURE `go`()
BEGIN
DECLARE d_z CONDITION FOR SQLSTATE '35241';
IF NOT EXISTS(SELECT * FROM _time) THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo
Let's give it a try
mysql> CREATE TABLE _time (`id` int);
Query OK, 0 rows affected (0.03 sec)
mysql> DELIMITER $$
mysql> CREATE PROCEDURE `go`()
-> BEGIN
-> DECLARE d_z CONDITION FOR SQLSTATE '35241';
-> IF NOT EXISTS(SELECT * FROM _time) THEN
-> SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
-> END IF;
-> END$$
Query OK, 0 rows affected (0.02 sec)
mysql> DELIMITER ;
mysql> CALL go();
ERROR 1644 (35241): errrrrrrrrrrrr
I'm just trying to create my first mysql stored procedure and I'm trying to copy some examples almost directly from the documentation, but it isn't working:
mysql> delimiter //
mysql> CREATE PROCEDURE ghost.test (OUT param1 INT) INSERT into admins SELECT COUNT(*) FROM bans; END//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; END' at line 1
What is the deal here? This is almost identical to:
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END//
Query OK, 0 rows affected (0.00 sec)
From
http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
Looks like you're missed the BEGIN.