pdo/mysql create procedure - mysql

I'd like to create a procedure from PHP using PDO...I can execute the procedure with PDO, I can create the procedure with the console line or with phpmyadmin, but I'm not able to create this very same procedure from PDO...
I absolutely need to be able to create this procedure from my php code (it's a setup part of a bigger program, the first step checks if the procedure exists with
SHOW PROCEDURE STATUS LIKE 'name_of_procedure'
if the procedure doesn't exists, I try (but fail) to create it (with prepare/execute couple of methods).

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 ;

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).

alternative for stored procedure in mysql which can be altered

1.) alternative for stored procedure in mysql which can be altered rather than drop and create again
2.) easy ways to maintain database version
i am currently creating log.sql files manually such as
example log.sql file
ALTER TABLE `ship`
CHANGE COLUMN `is_deleted` `is_deleted` INT(11) NULL DEFAULT 0 ;
DROP procedure IF EXISTS `get_ship`;
DELIMITER ;;
CREATE PROCEDURE `get_ship`(
)
BEGIN
do something;
end if;
END ;;
DELIMITER ;
Is there an better and easy way to create such sql log files,
I have already tried MYSQL workbench
Just as you create stored procedures using the CREATE PROCEDURE command, you alter them with ALTER PROCEDURE. The advantage of using ALTER PROCEDURE to change a stored procedure is that it preserves access permissions, whereas CREATE PROCEDURE doesn't. A key difference between them is that ALTER PROCEDURE requires the use of the same encryption and recompile options as the original CREATE PROCEDURE statement. If you omit or change them when you execute ALTER PROCEDURE, they'll be omitted or changed permanently in the actual procedure definition.
A procedure can contain any valid Transact-SQL command except these: CREATE DEFAULT, CREATE FUNCTION, CREATE PROC, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, CREATE VIEW, SET SHOWPLAN_TEXT, and SET SHOWPLAN_ALL. These commands must reside in their own command batches, and, therefore, can't be part of a stored procedure. Procedures can create databases, tables, and indexes, but not other procedures, defaults, functions, rules, schemas, triggers, or views
but u could try out views in mysql for u case

Firing a trigger when stored procedure execute

I am using SQL Server 2008. I have stored procedure, which (on some conditions) insert data in my table, this stored procedure will be call from application each time when my window opens. SQL Profiler shows it like below
exec TEST.dbo.spInsertRecords #parameter
Now what I want is to fire a trigger whenever this stored procedure will be call to execute.
Is it possible? If yes please share the syntax example.
Triggers are set on tables, if you really need something triggered upon calling your procedure (i honestly cannot see why since you shall be able to modify the procedure to do whatever behaviour you'd want), add some dummy table (a log table ?) , create your trigger on that table (on insert for example), and "trigger" the trigger inside your procedure (insert a row).
If the trigger is expected to execute some code, for example writing record into a log.
Write the code directly into the procedure.
If you want to execute universal code before or after all procedures, write the code in the place where the procedure is executed.
For example in PHP:
odbc_exec("execute my_trigger_before '$procedure_name' #parameters;
execute $procedure_name #parameters;
execute my_trigger_after '$procedure_name' #parameters;")

Determine the database name inside of stored procedure

I've created a stored procedure in Mysql and I was wondering if there is a command that I can get and store the name of the database that this stored procedure lives in?
How about DATABASE() ?
(This text is just filler)
Your Question is wrong?..Your actual question is you have created a stored procedure inside the databases...not how to retrieve the procedure inside the database?
Ans: To know the created procedure:
mysql:\> show procedure status; //to know the procedure name..sam4 function show function staus;
mysql:\> show use create procedure emptab.test1 //emptab is database name,test1 is the procedure..