Kindly help me to understand the reason to put delimiter (;) after every MySQL statement. Can we change default delimiter to some other character(like | ). And why it is not required to put ; after USE database command?
You change the delimiter with the delimiter statement. eg
delimiter //, then all your statements will need to be terminated with a //
this is necessary when defining stored procedures or triggers that require compound statements.
You can (obviously) change it back to a semi-colon with delimiter ;
use doesn't require a delimiter (in the command line client) because it's a cli command, not an sql command. (at least, according to this bug report)
Related
I currently have all of my sql queries written in my PHP files, within each class method. Is it possible to move all of these queries into stored procedures or stored functions in the database & simply pass the corresponding values / arguments into them from PHP?
I have read some of the documentation & it still appears unclear.
Thank you. :)
DELIMITER $$
create procedure `accounting`.`delete_invoice_line` (invoice_line_id INT)
BEGIN
delete from invoice_line where id = invoice_line_id;
END;
$$
DELIMITER ;
I had to figure the format for creating the procedure. I am following this pattern & it appears to be working properly. Then granting execute privileges for the user name.
Thank you all for your input. :)
Most queries can be moved into stored procedures, but probably not all of them. See mariadb's documentation on which SQL statements cannot be used in stored procedures:
ALTER VIEW; you can use CREATE OR REPLACE VIEW instead. LOAD DATA and
LOAD TABLE. INSERT DELAYED is permitted, but the statement is handled
as a regular INSERT.
LOCK TABLES and UNLOCK TABLES.
References to local variables within prepared statements inside a stored routine (use user-defined variables instead).
BEGIN (WORK) is treated as the beginning of a BEGIN END block, not a transaction, so START TRANSACTION needs to be used instead.
The number of permitted
recursive calls is limited to max_sp_recursion_depth. If this variable
is 0 (default), recursivity is disabled. The limit does not apply to
stored functions.
Most statements that are not permitted in prepared
statements are not permitted in stored programs. See Prepare
Statement:Permitted statements for a list of statements that can be
used.
SIGNAL, RESIGNAL and GET DIAGNOSTICS are exceptions, and may be
used in stored routines
Having said this, even though a SQL statement can be moved into a stored procedure, you may not necessarily want to do that due to code complexity or performance reasons.
I have a stored procedure designed to generate a new, 'derived' table. In this procedure I then want to add a column using ALTER TABLE. However, despite an almost identical stored procedure working fine, and despite being able to add this manually as a stored procedure to the database using MySQL Workbench, when I pass the code to the server using SOURCE (i.e. SOURCE workload.sql), I get an error 1146 (42502) 'Table 'workload._convenor_workload' doesn't exist.' (I'm doing this in Emacs as part of a org-babel block, but this is essentially just passing raw SQL to the server.)
As background, I'm in the process of migrating SQL code from a setting where I was running it raw to create my final database to one where I'd like this code to be called via triggers.
Setup: mysql Ver 8.0.16 for macos10.14 on x86_64 (MySQL Community Server - GPL)
I've tried rewriting this as a prepared statement, was unsuccessful, and have been scouring Stack Overflow. This is my first MySQL project and my reading of the documentation suggests that ALTER TABLE is a perfectly legal thing to do in a stored procedure. It's likely that I'm making a schoolboy error somewhere but at the moment I'm banging my head.
Elsewhere in my SQL, this code works in a stored procedure (ALTER TABLE function does not throw an error):
CREATE TABLE _assessment_allocations AS SELECT Assessment_ID,
IFNULL(SUM(_total_first_marking_hours),0) AS _total_first_marking_hours_sum,
GROUP_CONCAT(DISTINCT _total_first_marking_hours_needed) AS _total_first_marking_hours_needed,
GROUP_CONCAT(DISTINCT Prog_ID) AS prog_id
FROM
_marking_workload
GROUP BY Prog_ID, Assessment_ID;
ALTER TABLE _assessment_allocations
ADD COLUMN _assessment_variance DECIMAL(5,2);
However, the code that throws the error is this (specifically, the ALTER TABLE function; I've added the stored procedure code in case this is helpful). Note that this code does not throw an error when ingested by MySQL outside a stored procedure:
USE `workload`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `module_administration_convenor`()
-- Begin by selecting elements of the ~modules~ table
CREATE TABLE `_convenor_workload` AS
SELECT Modules.Module_Code,
Modules.Module_Name,
Modules.Module_Convenor_ID,
Modules.Module_Convenor_Share,
Modules.Student_Tally,
Modules.Additional_Hours,
Modules.Convening_Notes,
Modules.Active_Status
FROM modules;
-- Add a 'Convenor' column
ALTER TABLE `_convenor_workload` ADD COLUMN `Name` VARCHAR(255) DEFAULT 'Convenor';
\* Other stuff *\
END$$
DELIMITER ;
My aim is to avoid throwing this error. I'd like to get this stored procedure actually stored! (Just like the previous stored procedure that does much the same and does not throw an error.) I'm aware that there are some back-tick and style differences between the working and non-working code, but I'm guessing these aren't super important.
As I said, I have a strong suspicion that I'm overlooking something obvious here...
As mentioned by Solarflare in the comments, you are missing a begin so the alter table is executing as a separate action. If you wrap it with begin and end then it treats all the code as the stored procedure.
USE `workload`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `module_administration_convenor`()
Begin
-- Begin by selecting elements of the ~modules~ table
CREATE TABLE `_convenor_workload` AS
SELECT Modules.Module_Code,
Modules.Module_Name,
Modules.Module_Convenor_ID,
Modules.Module_Convenor_Share,
Modules.Student_Tally,
Modules.Additional_Hours,
Modules.Convening_Notes,
Modules.Active_Status
FROM modules;
-- Add a 'Convenor' column
ALTER TABLE `_convenor_workload` ADD COLUMN `Name` VARCHAR(255) DEFAULT 'Convenor';
END
$$
DELIMITER ;
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.
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 ;
In my model I defined some procedures. When I use the "Forward Engineering", in the generated every procedure gets a DELIMITER definition:
-- schema
CREATE DATABASE ...
CREATE TABLE foo ...
-- procedures
DELIMITER $$
...
BEGIN
...
END$$
DELIMITER ;
I need the SQL as input for the PDO#exec(...) and notice now, that the execution stops on the line of the first DELIMITER definition. But without defining delimiters it works.
How to use MySQL Workbench to generate SQL code with procedures without DELIMITER statements?