Why delimiter used with stored procedure in mysql? - mysql

I am trying to understand, why delimiter used with stored procedure in mysql?
but i couldn't.
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;`

Mysql's default delimiter is ; which is used for one statement in the command line , something as
select * from users ;
When you write a trigger or stored procedure to execute the entire code mysql needs to understand that its a block of code/query.
If no delimiter is provided then when mysql encounters any ; inside the store procedure or trigger it will think that as one statement and will try to execute it. So we need to provide a delimiter for store procedure or trigger and make mysql understand that anything within that delimiter is one complete set of code.
So in your example
SELECT * FROM products;
it will be a part of the complete statement when there is a delimiter other than ; is provided at the beginning.

Related

As is not valid identifier when creating procedure

I was following a tutorial on how to create procedures, I wrote the code
CREATE PROCEDURE test2
AS
select * FROM vgsales_ratings;
and got
"AS" is not valid at this position expecting '('
I tried adding parentheses around some of the stuff but it didn't change anything. Is something wrong with my machine or is the code wrong. I'm using mysql workbench 8. The table exsits. Thanks for any help
I would simply use :
CREATE PROCEDURE test2 ()
select * FROM vgsales_ratings;
But most of time,we use a routine block if we have multiple statements. In this case, delimiter needs to be used to change the default delimiter. After the procedure is created, change the delimiter back to ; .
delimiter //
CREATE PROCEDURE test2 ()
BEGIN
IF (select count(*) FROM vgsales_ratings) > 0 then
select * FROM vgsales_ratings;
end if;
END //
delimiter ;

Cannot create stored procedure

I have the following piece of statement entered into MySQL5.6 Command Line Client. However, the following error was received. I haven't even been able to add in END// Delimiter; after the select statement.
At the same time, i was wondering after the stored procedure has been created successfully, how do i CALL the stored procedure without the command line but using java codes.
Kindly assist. Greatly appreciated!
give space between delimiter and //. After your select statement write end; on next line and // at last line (after end; in next new line)
delimiter //
create procedure GetUStocks()
Begin
Select * From buystocks;
end;
//
mysql> delimiter //
mysql> CREATE PROCEDURE GetUStocke()
-> BEGIN
-> SELECT * FROM buystocks ;
-> END//
You need a space between DELIMITER and the symbol you are changing the delimiter to.
mysql> DELIMITER //
The clue that it worked should be that you get another mysql> prompt instead of the "unfinished command" prompt ->.
Re your comment, if you need to call a stored procedure from a Java application, refer to the manual on callable statements: http://dev.mysql.com/doc/refman/5.6/en/connector-j-usagenotes-statements-callable.html
Well, seriously, I was Shocked and still am upon this accidental discovery.
It's simply because you are not using the delimiter that you have defined for ending the procedure.
Here let me attach two snippets that will help illustrate what is generating the error.

trigger in MYSQL CONSOLE

I am working on phpmyadmin.
To write triggers i am using mysql console.
Its works well in starting. but as soon as i am writing a trigger and its gets execute successfully then every time after any query i have to give delimiter (|) to execute the query.
I am not able to understand why i have to put delimiter after a simple select query? Delimiter is for trigger rite.
Am i missing something in writing trigger?
for exmp:
after a trigger i am writing select statement than i have to write it as:
select * from tableName;|
If i am not using | its not getting execute.
Try this :
DELIMITER $$
DROP TRIGGER IF EXISTS `myTriggerName`$$
CREATE TRIGGER `myTriggerName` AFTER DELETE ON `myTableName` FOR EACH ROW BEGIN
...........
............
.............
END$$
DELIMITER ;
After the trigger is created, you need to change the delimiter back to ';'
See examples in the manual:
http://dev.mysql.com/doc/refman/5.6/en/stored-programs-defining.html
delimiter //
... your trigger here ...
delimiter ; <-- change the delimiter back

can procedures within mysql triggers work?

Can a stored procedure plus a sql query be executed from within a trigger?
procedure plus query
SET #tmp = (SELECT fvalue FROM ftable WHERE id=2);
SET #SQL = CONCAT('INSERT INTO ',#tmp,' VALUES (''buhambug'')');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
If so what are the rules and links to examples? I have't been able to find any.
Yeah, you can call a stored procedure from a trigger like this:
Create trigger foo after insert on table for each row
BEGIN
call your_procedure(params);
END$$
Note the ending delimiter. If ; is the default delimiter inside the trigger as well, then it won't work, for MySQL will treat each command separately and throw error. You want the entire code inside the trigger to be executed together. hence declare a different delimiter, like $$ prior to defining the trigger, through Delimiter $$. Then, ; will work correctly inside the the trigger. After you terminate the trigger through $$, don't forget to restore the default delimiter.

i bulid stored procedure in MySql server but doesn't work !

how to bulid a stored procedure in MySql server .
i wrote the stored procedure outside in note but i don't know how to apply this stored procedure in the server that i am doing the testin .
to be clear i download wampSever which includes Mysql ,then i created my tables. but again i don't know the steps that i have to take in orded to use the stored procedure.
my stored procedure:
CREATE PROCEDURE findHorse
#horseName varchar(15) AS SELECT * FROM horse
WHERE horseName =#horseName
i know that if i wante to execute it i use
exec findHorse
all what i need are :
1. how to create it in mySql. //steps
2.where should i put exec findHorse in order to execute it //steps
In MySQL you execute a stored procedure by 'calling' it:
CALL DoStuff(#Param1);
You create one as follows:
CREATE PROCEDURE DoStuff()
BEGIN
SELECT Column1
FROM MyTable;
END;
This might help
I believe this is what you are looking for:
DELIMITER $$
CREATE PROCEDURE `findHorse` (IN `inFindHorse` CHAR(255))
BEGIN
SELECT * FROM `horse` WHERE `horse`.`horseName` = `inFindHorse`;
END$$
DELIMITER ;
And as Randy noted, the correct way to call this procedure is:
CALL findHorse('Flicka');