Trouble creating stored procedure - mysql

I'm messing around with stored procedures for the first time, but can't even create a simple select! I'm using phpMyAdmin and this is my SQL:
DELIMITER //
CREATE PROCEDURE test_select()
BEGIN
SELECT * FROM products LIMIT 10;
END //
DELIMITER ;
After submitting that, my localhost does some thinking for a loooong time and eventually loads a page with no content called /phpmyadmin/import.php. After reloading phpMyAdmin and trying to invoke the procedure:
CALL test_select();
I get a "PROCEDURE doesn't exist" error. Any ideas?

Try to use the delimiter field of phpMyAdmin, as shown in the screenshot below:
Simply put the following in the query window:
CREATE PROCEDURE test_select()
BEGIN
SELECT * FROM products LIMIT 10;
END
In addition note that there is bug in some older versions of phpMyAdmin, which can cause an error when you call stored procedures that contain SELECT statements from phpMyAdmin.
You may want to check out the following posts for further reading:
MySQL Stored Procedures not working with SELECT (basic question)
How do I write an SP in phpMyAdmin (MySQL)?
This bug effects only phpMyAdmin, and you would still be able to call the stored procedure from anywhere else.

Related

SQL error when I try to ALTER TABLE in a stored procedure

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 ;

Working with Events in MySQL

I have a stored procedure which basically selects data from one table and insert into another table. Basically I am doing data archiving manually. Now, I want to write an event just like discussed here
However, after reading that post and researching online, I came to know that it's not possible to create an event inside a stored procedure. Is there a way to accomplish my goal in MySQLWorkbench?
I believe you are thinking this in the oposite direction: You can't create an event in a stored procedure, but you can create a stored procedure and call it from an event.
Example:
delimiter $$
create procedure myProc()
-- Dummy procedure to move the data from mainTable to backupTable,
-- and then clear (truncate) mainTable
begin
insert into backupTable select * from mainTable;
truncate mainTable;
end $$
delimiter ;
-- Now, suposing that you want to execute this procedure every hour:
delimiter $$
create event myEvent
on schedule every 1 hour
do
begin
call myProc();
end $$
delimiter ;
You can write this as any other query in the workbench, or directly in the command line client.
About your concern
After reading your comment, I believe you are a bit confused about what MySQL Workbench is.
MySQL Workbench is only a graphical application that allows you to connect to a MySQL server and perform queries and administration tasks. But Workbench is not the core of MySQL... it is only a viewer (with steroids, maybe, but a viewer after all).
Now, the event scheduler does not reside in Workbench, but in the MySQL server instance you are connecting to. Just as the tables, views, procedures and functions are not stored in the Workbench interface but in the server, the events are also stored in the server.
(Yes, I believe it is a relevant SNAFU that the scheduled events don't show anywhere in the graphical interface, but... after a while, one learns to live with that kind of frustrations and to move on with life)
Maybe your only concern is: "Hey, and what if I want to know what events are set to run in the event scheduler?" You can execute a "show events" query to show a list of the events in the current database, and you can execute "show create event yourEvent" to show the create event syntax for that event.
I insist: Read the manual, and keep a copy at hand (download the manual for your MySQL version here).

get records from procedure having multiple select statements in mysql

I have created a procedure in mysql having multiple select statements in it.
Here is my code :
DELIMITER $$
USE `databasename`$$
DROP PROCEDURE IF EXISTS `wholeProjectDetails`$$
CREATE DEFINER=`databasename`#`localhost` PROCEDURE `wholeProjectDetails`(IN givenpid INT)
BEGIN
select * from projects where projectid=givenpid;
select * from projects where projectid<>givenpid;
END$$
DELIMITER ;
When i called this procedure using statement :
call wholeProjectDetails(2);
it is displaying only first statement's results, i want that it will display both statement's records.
Please let me know what i am doing wrong?
Thanks
When you call this procedure, MySQL creates two result-sets. Now, you need to get these two result-sets using your MySQL client. Read information about client you use, does it support this feature? For example, in .NET you can use IDataReader.NextResult Method, in mysqli - mysqli::next_result function, and so on.
If you want just to view these result-sets, you can install one of MySQL GUI tools, there are free ones.

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 insert the SQL Error message into table

I want to store the error messages which occurs while executing the stored procedure in another error table.
Here is the my sample procedure having some error statements.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`$$
CREATE PROCEDURE `test`()
BEGIN
SELECT * FROM emp;
END $$
DELIMITER $$;
When i call the above procedure it gives me error.I wnat to store this error code & message in another table as "error".
Any pointers are appreciated.
Thanks in Advance.
The standard way to handle this in MySQL is to declare a HANDLER to handle the error condition the way you want. This allows you to insert an error message into another table if you want, and then to either CONTINUE or EXIT the running procedure as required.
Here's the documentation:
http://dev.mysql.com/doc/refman/5.0/en/declare-handler.html
Unfortunately you will not be able to access the SQLSTATE of the statement that caused the error, so this approach is somewhat limited.
Here's another relevant question on SO with much more detail:
MySQL Stored Procedure Error Handling
SQL ERROR means there is something wrong with your SQL Query. It may (or may not) depend on your table. If the error is TABLE SPECIFIC or QUERY SYNTAX ERROR, that is, if there is something wrong with a specific table ONLY, or with a query, then definitely you can insert your errors into a table. But, if there is something with your CONNECTION or something else, then you cannot insert errors into any table.
ALWAYS try to log your errors in a html file or txt (text) file, so that you can smoothly access it. Also there is a less chance for failure.