mySql :
Your SQL query has been executed successfully
0 rows affected by the last statement inside the procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `update_adm`(OUT `sp_out` INT(11), IN `sp_email` VARCHAR(50) CHARSET utf8)
NO SQL
BEGIN
UPDATE `admin` SET `last_try`=curtime() WHERE `email`=sp_email;
SET sp_out=ROW_COUNT();
END
why 0 rows affected by the last statement inside the procedure ?!
edited :
When I replace sp_email with correct value like navid#yahoo.com in my stored procedure, it works perfectly !
CREATE DEFINER=`root`#`localhost` PROCEDURE `update_adm`()
NO SQL
BEGIN
UPDATE `admin` SET `last_try`=curtime() WHERE `email`='navid#yahoo.com';
END
From the MySQL command line, the output is as expected.
mysql> delimiter $$
mysql> CREATE DEFINER=`root`#`localhost PROCEDURE update_adm( ...
-> END$$
Query OK, 0 rows affected (0.16 sec)
mysql> delimiter ;
mysql> insert into admin values (null,'foo');
Query OK, 1 row affected (0.00 sec)
mysql> call update_adm(#cnt,'foo');
Query OK, 1 row affected (0.00 sec)
mysql> call update_adm(#cnt,'bar');
Query OK, 0 rows affected (0.00 sec)
When I call the procedure with an email that exists, I get a message back showing 1 row was affected. When I call the procedure with an email that does exist, it returns a message back showing 0 rows affected.
I believe the message you are seeing is from the client interface. What client are you using to call the procedure?
Related
I want to be able to create some stored Procedures in different databases - so I want to be able to pass in the database name into the stored procedure creation statement.
SET #SourceDBName='dev';
DELIMITER //
CREATE PROCEDURE test ()
BEGIN
USE #SourceDBName;
SELECT * FROM agreed_relation;
END //
DELIMITER ;
How can I pass #SourceDBName into the CREATE PROCEDURE statement?
to pass in the database name into a stored procedure you must declare it in the procedure like this:
DELIMITER //
CREATE PROCEDURE test (IN idbname VARCHAR(20))
BEGIN
SELECT * FROM agreed_relation WHERE dbname = idbname;
END //
DELIMITER ;
After you create a Table like this where 'dbname' field it's the one your procedure will call:
create table agreed_relation
(
dbname varchar(30) not null
);
After insert some values you call the procedure and pass any dbname to the SELECT statament:
insert into agreed_relation(dbname) values('Oracle');
insert into agreed_relation(dbname) values('Mysql');
insert into agreed_relation(dbname) values('Mongodb');
Calling procedure and passing some value:
CALL test('Mysql');
14.1.16 CREATE PROCEDURE and CREATE FUNCTION Syntax
...
USE statements within stored routines are not permitted. When a
routine is invoked, an implicit USE db_name is performed (and undone
when the routine terminates). The causes the routine to have the given
default database while it executes. References to objects in databases
other than the routine default database should be qualified with the
appropriate database name.
...
An option to create the stored procedure in different databases is:
FIle: /path/to/file/myProcedure.sql
DROP PROCEDURE IF EXISTS `test`;
DELIMITER //
CREATE PROCEDURE `test`()
BEGIN
SELECT * FROM `agreed_relation`;
END//
DELIMITER ;
MySQL Command-Line:
mysql> USE `dev`;
Database changed
mysql> \. /path/to/file/myProcedure.sql
Query OK, 0 rows affected (0.00 sec)
mysql> USE `db1`;
Database changed
mysql> \. /path/to/file/myProcedure.sql
Query OK, 0 rows affected (0.00 sec)
mysql> USE `db2`;
Database changed
mysql> \. /path/to/file/myProcedure.sql
Query OK, 0 rows affected (0.00 sec)
mysql> USE `dev`;
Database changed
mysql> CALL `test`;
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> USE `db1`;
Database changed
mysql> CALL `test`;
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> USE `db2`;
Database changed
mysql> CALL `test`;
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
You can also create the stored procedure in a single database and pass the database name as a parameter to qualified the required objects and use 14.5 Prepared SQL Statement Syntax to execute it:
mysql> USE `dev`;
Database changed
mysql> DROP PROCEDURE IF EXISTS `test`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `test`(`db_name` VARCHAR(64))
-> BEGIN
-> SET `db_name` := TRIM('\'' FROM QUOTE(`db_name`));
-> SET #`query` := CONCAT('SELECT * FROM `', `db_name`, '`.`agreed_relation`');
-> PREPARE `stmt` FROM #`query`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `test`('dev');
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> CALL `test`('db1');
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> CALL `test`('db2');
Empty set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
I'm struggling with a simple stored procedure, that I've reduced to this:
CREATE PROCEDURE RemoveDuplicateModules()
BEGIN
SET #myvar=1;
End;
When I run this in the MySQL CLI I get: parameter #myvar has not been created. I'm struggling cause in windows it works fine!
mysql --version = 5.6.33
Works for me (debian, mysql 5.6.25):
mysql> delimiter //
mysql> CREATE PROCEDURE RemoveDuplicateModules()
-> BEGIN
-> SET #myvar=1;
-> End;
-> //
Query OK, 0 rows affected (0,66 sec)
mysql> call RemoveDuplicateModules();
Query OK, 0 rows affected (0,00 sec)
mysql> select #myvar ;
+--------+
| #myvar |
+--------+
| 1 |
+--------+
1 row in set (0,02 sec)
Seems that the problem here was not that the procedure I posted was failing, but since it was being executed in a larger context, a big file with a lot of code from a lot of people, a previous lack of a commit made my procedure to fail. Anyway, thanks for the help!
I've inherited some code that uses stored procedures and one of them doesn't appear to be working correctly.
The stored procedure uses a temporary table to insert data later in the procedure. However when I execute the stored procedure, no data is inserted. When I debug it, I get the error:
Table 'db.testtable' doesn't exist
I've stripped down the stored procedure to the following code, and it doesn't work. I always get the error on the SELECT statement. Everything looks OK from what I can tell based on the examples I've seen.
DROP PROCEDURE IF EXISTS db.insert_record;
CREATE PROCEDURE db.`insert_record`(id int, status int)
BEGIN
DECLARE code varchar(45);
DROP TEMPORARY TABLE IF EXISTS testTable;
CREATE TEMPORARY TABLE testTable AS (SELECT 'TEST' AS fakeColumn);
SELECT fakeColumn INTO code FROM testTable;
END;
I've also verified that the user I am connected as has the permission to create temporary tables; in fact it has every permission available
Additional Details
Running MySQL 5.6 on Windows.
If I take the drop / create / select statements by themselves and run as a script, it behaves as expected.
Using Toad for MySQL to debug the stored procedure.
I can't reproduce the problem. What other information can you provide?
mysql> USE `test`;
Database changed
mysql> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 5.5.22-0ubuntu1 |
+-----------------+
1 row in set (0.00 sec)
mysql> DELIMITER //
mysql> DROP PROCEDURE IF EXISTS `insert_record`//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE `insert_record`(`id` int, `status` int)
-> BEGIN
-> DECLARE `code` VARCHAR(45);
-> DROP TEMPORARY TABLE IF EXISTS `testTable`;
-> CREATE TEMPORARY TABLE `testTable` AS (SELECT 'TEST' AS `fakeColumn`);
-> SELECT `fakeColumn` INTO `code` FROM `testTable`;
-> SELECT `code`;
-> END//
Query OK, 0 rows affected (0.01 sec)
mysql> CALL `insert_record`(NULL, NULL)//
+--------+
| `code` |
+--------+
| TEST |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
SQL Fiddle demo
I am creating procedure in mysql. but I am facing some issues while creating that.
I am applying query i.e
CREATE PROCEDURE simpleproc( param1 INT) BEGIN SELECT COUNT(*)
INTO param1 FROM 91_nidhi; END//
and The error is
#1064 - You have an error in your SQL syntax; c
heck the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 1
This how you need to do
- You are not passing any input value rather you are using an output value
- so specify the param as OUT
Below is the example
mysql> create table 91_nidhi (id int,val varchar(20));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into 91_nidhi values (1,'aa'),(2,'bb'),(3,'cc');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
Now lets create the procedure
delimiter //
CREATE PROCEDURE simpleproc( out param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM 91_nidhi;
END; //
Then change the delimiter in the terminal as
mysql> delimiter ;
Now lets call the procedure
mysql> call simpleproc(#res);
Query OK, 0 rows affected (0.00 sec)
mysql> select #res ;
+------+
| #res |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
I have an error while executing this query:
CREATE TRIGGER insert_Topics
BEFORE INSERT
ON Topics
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM Subjects WHERE ID=new.SubjectID)=0
THEN
INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
delimiter ;
The error says:
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 '' at line 8
and it points to the word THEN
Any help? Thanks in advance.
Both
CREATE TRIGGER insert_Topics
BEFORE INSERT
ON Topics
FOR EACH ROW
BEGIN
IF NOT EXISTS(SELECT 1 FROM Subjects WHERE ID=NEW.SubjectID LIMIT 1) THEN
INSERT INTO error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
and
CREATE TRIGGER insert_Topics
BEFORE INSERT
ON Topics
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM Subjects WHERE ID=NEW.SubjectID LIMIT 1) IS NULL THEN
INSERT INTO error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
work for me, so your syntax problems probably lie elsewhere.
I know this is an old question, but I thought I'd share my thoughts over what I think the error was, in case anyone sees this in the future (feel free to community-edit if i've made any mistakes!).
The reason there were issues in the original question seems to be with the use of DELIMITER, and what it does, as explained here: What does DELIMITER // do in a Trigger?
In the given code:
INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
delimiter ;
The resetting of delimiter with "delimiter ;" indicates that it changed previously to another delimiter, say:
DELIMITER //
--TRIGGER1
--TRIGGER2
--TRIGGERn
delimiter ; -- Aggravates me that this wasn't capitalised :P
The reason for the delimiter to be set to "//" (or others), is to allow multiple statements to be delimited, and what I'm thinking, is that the original author had other triggers above that trigger, and had changed the delimiter to something like "//", but had forgotten to change:
END IF;
END;
delimiter ;
To:
END IF;
END//
delimiter ;
Therefore, the reason why commenters weren't able to replicate, was because they had the correct delimiter set (";"), and as there was only one statement to delimit, didn't find any errors, as the author did.
See the following test code and results, including initialisation, as some proof:
DELIMITER ; -- Just in case
DROP TABLE IF EXISTS Topics, Subjects, error_msg;
CREATE TABLE Subjects ( id INT(5) PRIMARY KEY,
subjectname CHAR(20));
CREATE TABLE Topics ( topicname CHAR(20),
subjectID INT(5)
# FOREIGN KEY (subjectID) REFERENCES Subjects(id) - How this check should be done...
);
CREATE TABLE error_msg ( Message VARCHAR(50) );
INSERT INTO Subjects VALUES
('5', 'Arts'),
('55', 'Maths'),
('2342', 'Biology'),
('12345', 'Finance');
DELIMITER $$ -- Where the delimiter would be defined, originally
CREATE TRIGGER InsertOnTopics
BEFORE INSERT ON Topics
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM Subjects WHERE id=NEW.subjectID)=0 THEN
INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END$$
DELIMITER ;
INSERT INTO Topics VALUES
('Welfare Benefits', '5'),
('Eigenvectors', '55'),
('Mitochondria', '2342'),
('Bank of Dad', '12345'),
('Something else', '555');
SELECT * FROM error_msg;
With "end;", as the original code:
mysql> SOURCE /Users/benpalmer/test.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.02 sec)
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 '('Something else', '555')' at line 6
Empty set (0.00 sec)
mysql>
With "END$$", in my code:
mysql> SOURCE /Users/myUsername/test.sql
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.02 sec)
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
+----------------------------------+
| Message |
+----------------------------------+
| Foreign Key Constraint Violated! |
+----------------------------------+
1 row in set (0.00 sec)
mysql>
Other than this, I don't understand why this particular example isn't being done with a proper foreign key constraint. As my first answer... Hope this helps anyone! Was also stuck on this for a while.