I need to echo a statement in stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `Edit_table` $$
CREATE PROCEDURE `Edit_table` (in_db_nm varchar(20),in_tbl_nm
varchar(20),in_your_query varchar(200)) DETERMINISTIC BEGIN
select concat('Table ',in_tbl_nm, ' not found');
END $$
DELIMITER ;
this is what I get from the console when running it, it seems to always print the first line because that's the table column name, is there a way to remove this?
concat('Table ',in_tbl_nm, ' not found')
Table xxxxx not found
DELIMITER $$
DROP PROCEDURE IF EXISTS Edit_table $$
CREATE PROCEDURE Edit_table (in_db_nm varchar(20),in_tbl_nm
varchar(20),in_your_query varchar(200))DETERMINISTICBEGIN
select concat('Table ',in_tbl_nm, ' not found') as DisplayData;
END $$
DELIMITER ;
This changes the header to 'DisplayData'
You can also start MySQL with --skip-column-names option to hide the column names
Related
Im trying to add parameter to this stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test` $$
CREATE PROCEDURE `mifostenant-default`.`test` ()
BEGIN
select * from employees
END $$
DELIMITER ;
Here is the way to make a procedure and pass a parameter to it:
delimiter $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test`;
CREATE PROCEDURE `mifostenant-default`.`test` (IN empName CHAR(20))
BEGIN
SELECT * FROM employees WHERE employee_name=empName;
END $$
delimiter ;
And you call your procedure as below:
CALL mifostenant-default.test(the_parameter_to_pass);
I write a simple mysql stored procedure in workbench, however, it complains syntax error. What is wrong with my syntax?
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END
Check out this link for a simple example of a Stored Procedure.
To your question: When you define a SP, you always start with setting a new delimiter in order to use the normal delimiter (;) in your SP. If you don't do this, SQL thinks that you finished your query after the smicolon, which isn't at the end of your query so it throws an error. To set a new delimiter, do the following:
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * FROM indicators;
...
END //
DELIMITER ;
So now, you first set the delimiter to // and sql just stores the simicolons in your SP. At the END, you say that your query is done and type // - the new delimiter. Then you set it back to the normal simicolon in order to continue as always.
You'd better post your error messages, even though I can guess the problem you are suffering.
The default delimiter of MySQL is ";". So MySQL Workbench treat the statement below as a complete statement:
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
It's definitely wrong!
So you should change the delimiter expilictly when you write procedures. Here is an example:
delimiter // -- change the delimiter temporarily
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END //
delimiter ; -- restore the default delimiter
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;
Use this
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;
I am trying to run a query which I first typed out like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$
CREATE PROCEDURE development()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 500 DO
IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF;
SET i = i + 1;
END WHILE;
END $$
CALL development() $$
DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
but then "compressed" into this:
DELIMITER $$ DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$ DELIMITER ;
on one line. The problem is that the first (multiline) code works, and does what it's supposed to do, while the other (single line) version, doesn't. It doesn't fail or throw errors, it just doesn't insert the rows like the multiline version. Why is this? More importantly, how can I make the single line version work?
Thanks in advance!
please try it this way:
DELIMITER $$
DROP PROCEDURE IF EXISTS development $$ CREATE PROCEDURE development() BEGIN DECLARE i INT DEFAULT 1; WHILE i <= 500 DO IF NOT EXISTS ((SELECT * FROM customers WHERE customerID = 500)) THEN INSERT INTO customers (firstName) VALUES (''); END IF; SET i = i + 1; END WHILE; END $$ CALL development() $$ DROP PROCEDURE IF EXISTS development $$
DELIMITER ;
The last line break might not be neccessary.
But I am fairly confident that, when inlining the whole code you end up setting a delimiter much longe than "$$"
I just executed the completely inlined example on a random sql server of mine and always received a positive result, even though I have not set up the related db structure.
with line breaks there are complaints about missing tables (as would be expected)
Regards
Stefan
You cannot run any SQL on the same line as delimiter. delimiter is a built in mysql client command that sets the statement seperator, not an sql interpreter. It takes one argument, read up to the first space or newline. Everything else is ignored.
delimiter $$ select now() $$
No output, because everything after $$ is thrown away.
Or illustrated by bad syntax
delimiter "$$" select; nothing order by from nowhere group , oh forget it $$
I have following store procedure. It is give me some error
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg)
END
END$$
DELIMITER ;
It is giving me missing semicolon error. Don't know Why this error is getting. Can someone help me?
Try like this:
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg);
END$$
DELIMITER ;
There's only one BEGIN and two ENDs, remove the 2nd END and you should be fine.
Replace root#localhost with root#localhost
I would like to execute a loop in phpmyadmin which inserts rows in a table. So far I have:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows()
With this, I get an 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 'DELIMITER$$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE ins' at line 1
Syntax for the DROP PROCEDURE statement is incorrect!
Change
DROP PROCEDURE IF EXISTS insert_my_rows()
to
DROP PROCEDURE IF EXISTS insert_my_rows;
You need to end the statement with the proper delimiter. Change END to END$$.
You must change the delimiter only while you make blocks of statements, so during the procedure definition. The DROP PROCEDURE and CALL statements needs delimiters too.
DROP PROCEDURE IF EXISTS insert_my_rows;
DELIMITER $$
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows();
DROP PROCEDURE IF EXISTS insert_my_rows;