Can create procedure without using delimiter in MySQL? - mysql

I have a question about creating stored procedure in MySql without using delimiter
I search a lot in web but i dont find anything usefull for finding my answer.
so can we ever dont use delimiter??
if yes, how?
I am so happy if anyone can help me here with this question
thank you

If the procedure is just a single statement, you don't need to change the delimiter.
CREATE PROCEDURE myProc(param INT)
SELECT col1, col2 FROM someTable WHERE col3 = param;
You only have to change the delimiter if the procedure consists of multiple statements, since ; is the statement terminator in the procedure. If you don't change the delimiter, it will be treated as the terminator of the CREATE PROCEDURE statement itself.

Related

mysql stored procedure and functions syntax errors

I'm working in mysql workbench 8.0.11 and having problems creating stored procedures and also functions. Just to be clear, I am running this in mysql via the mysql workbench. I am not using any other program at all.
I'm trying to create a new stored procedure. I keep getting a syntax error: "delimiter" is no valid input at this position, expecting: CREATE.
I have checked the mysql website to make sure my syntax is correct, and it matches. Another thing to note is that I've tried starting the stored procedure with use accounting; (the db I'm working with) and I get the same error, except that "use" takes the place of "delimiter". So, I'm not sure it has anything to do with the delimiter keyword itself. Is there some setting in mysql workbench that I can set to get this straightened out? Also, I get the same exact syntax error when trying to create a function.
I have tried creating both a stored procedure and a function without using the delimiter keyword, or the use keyword, and when I hit the apply key mysql crashes.
Here is my code:
delimiter $$
create procedure 'add_expense_category' (id int, name varchar(20))
begin
insert into expense_categories(expense_category_id, expense_category)
values(id, name);
end $$
delimiter ;
Anyone have any ideas on how to solve this? Settings to change? Anything?
Thanks!
try this
delimiter $$
create procedure `add_expense_category` (id int, name varchar(20))
begin
insert into expense_categories(expense_category_id, expense_category)
values(id, name);
end $$
delimiter ;

mysql if without select

I'm using mysql and I want to use the if statement in a way you are doing in a stored procedure. Something like this:
delimiter $$
if #myVariable is null then
drop temporary table tmp_buffer;
select 'cannot proceed without variable #myVariable';
else
update my_table as t
set t.name = #myVariable;
end;
end$$
delimiter ;
When I execute this code it does nothing.
I tried to google this but I only find select if(....) explanations what doesn’t fit to my requirements.
I know that it is possible to create a stored procedure and put the code there and then call the procedure, but I'm looking for a way to do it without stored procedures.
Is this possible? If yes, what is wrong in my code?
Thanks for reading this
Felix
but I'm looking for a way to do it without stored procedures. Is this
possible?
NO; as already commented above, you cann't use if .. else construct block like the way you are intend to use in a normal SQL query. You will have to wrap it inside a procedural block which could be a stored procedure or a function.

Create a stored procedure only if the procedure does not exist in mysql

In SQL Server I am able to achieve this using dynamic sql string, but now I need to do the same thing for mysql but am getting nowhere, is there any way to achive this
IF NOT EXISTS (SELECT 1 FROM mysql.proc p WHERE NAME = 'stored_proc_name')
BEGIN
DELIMITER $$
CREATE PROCEDURE justATest()
BEGIN
-- some SP logic here
END$$
END
I am storing the whole sql as a string inside a database column and execute the statement using a prepared statement Execute inside another stored procedure.
IF NOT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name') THEN
....
taken from
Older Post
Control statements like if then else are only allowed inside Stored Procedures in MySQL (unfortunately). There are usually ways around this, but it depends why you are conditionally creating the sproc.
E.g. If you're trying to avoid errors when running build scripts because sprocs already exist then you can use a conditional drop statement prior to your create like this:
DROP PROCEDURE IF EXISTS justATest;
CREATE PROCEDURE justATest()
BEGIN
-- enter code here
END;
This will ensure the any changed code gets run (rather than skipped).

How to return a table from Stored Procedure in MySql?

i found my answer in sql server at here, but my question is about in mysql. please help me
well to create a stored procedure in mysql you do it like so...
delimiter //;
create procedure Foo()
BEGIN
Select * from videos;
END
See http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html for additional details

What is the equivalent of Oracle’s REF CURSOR in MySQL?

I am trying to make a procdure in mysql that returns me an array with the result, I used to do with the oracle ref cursor, but in mysql do not know how to proceed,
I have to pass parameters too...
Anyone know how I can do, or have an example to show me? Thank you very much...
MySQL doesn't have a refcursor like Oracle. If you are planning to write a stored procedure that returns multiple rows/result set in MySQL just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
and call sample();. It will return a result set which you can use.
There is no analog of REF CURSOR in MySQL. Stored procedures and functions allow to pass and return only scalar datata types, see the reference here - CREATE PROCEDURE and CREATE FUNCTION Syntax.
MySQL cannot operate with arrays. A workaround is to use a table (or TEMPORARY TABLE).
Also - take advantage of visual object editors and stored procedure debugger in dbForge Studio for MySQL.