MySQl IF NOT EXISTS, CREATE Stored procedure - mysql

I come from a background with Microsoft SQl using their Server management studio. I have recently switched to mysql and am looking to create a stored procedure with the same method I use with MSSQL. I want to create a procedure if it does not exists because I prefer that to dropping if exists. Below is the syntax I would use in MSSQL. Any help would be much appreciated.
IF NOT EXISTS
(
SELECT
1
FROM
sysobjects WITH (NOLOCK)
WHERE
[type] = 'P' AND name = 'Sproc'
)
EXEC('CREATE PROCEDURE dbo.Sproc AS BEGIN SET NOCOUNT ON; END')
GO
ALTER PROCEDURE dbo.Sproc
(
#Sproc_Params
)
AS
BEGIN
.... --Sproc code
END

You can look in mysql.proc to see if a procedure exists.
SELECT db, name FROM mysql.proc WHERE db = 'dbo' AND name = 'Sproc';
However, in MySQL, you cannot ALTER PROCEDURE to replace the procedure body. You can only change a few attributes of the procedure. See http://dev.mysql.com/doc/refman/5.7/en/alter-procedure.html for details.
So you will have to drop and create the procedure anyway, if you want to change its parameters or its body.

Related

Attempting to insert into a table then update a field within the same stored procedure

I'm attempting to create a procedure that is a basic insert into a table, and then performs a quick update on another table afterwards in MySQL. Please find the code below:
DROP PROCEDURE IF EXISTS `sp_insertUserSocial`
GO
CREATE PROCEDURE sp_insertUserSocial
(
IN p_userSocialID INT(11),
IN p_socialID INT(11),
IN p_userID INT(11),
IN p_referralID INT(11)
)
BEGIN
INSERT INTO UserSocial
(
userSocialID,
socialID,
userID,
referralID
)
VALUES
(
IN p_userSocialID,
IN p_socialID,
IN p_userID,
IN p_referralID
) ;
UPDATE Users
SET connCount = connCount + 1
WHERE UserID = p_referralID;
END
GO
In PHPAdmin it's giving me a syntax error, but I'm not sure where exactly it is? It says line 23, which makes me think it's the semi-colon but I thought that these were needed after an insert statement?
Any help is appreciated, thanks!
I see a couple of problems:
GO, must specify the DELIMITER. 19.1. Defining Stored Programs.
In the INSERT (13.2.5. INSERT Syntax), the IN is optional to pass parameters to the stored procedure (13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax), but not part of the syntax of the INSERT.
SQL Fiddle demo

How to update/create a mysql stored procedure using mysql query?

My Question is: How to update or create MySQL stored procedure via VB.Net ?
Example:
MySQL Stored Procedure:
CREATE DEFINER=root#localhost PROCEDURE SP_tst()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'testing'
BEGIN
Select * from table1;
END
How to create the above stored procedure via VB.Net ?
(like we create / alter a table via VB.Net)

Why does the CREATE PROCEDURE statement fail when I use it with IF Statement here?

I am trying to DROP a stored procedure if it exists and then CREATE it by doing this way:
IF OBJECT_ID('[dbo].[myStoredProc]') IS not NULL
DROP PROCEDURE dbo.myStoredProc
CREATE PROCEDURE [dbo].[myStoredProc]
(
#parameter1 BIT
) AS
IF #parameter1 = 1
BEGIN
....
But it complains that :
"CREATE PROCEDURE must be the only statement in the batch"
Question: How can I fix my script to overcome this?
You need to put a go at the end of your first logical batch.
IF OBJECT_ID('[dbo].[myStoredProc]') IS not NULL
DROP PROCEDURE dbo.myStoredProc
go -- you need to add the batch-terminator 'go'
CREATE PROCEDURE [dbo].[myStoredProc]
(
#parameter1 BIT
) AS
IF #parameter1 = 1
BEGIN
..
Adding GO after your IF statement shows that this is the end of your first query batch.
Read more here:
http://msdn.microsoft.com/en-us/library/ms188037.aspx
IF OBJECT_ID('[dbo].[myStoredProc]') IS not NULL
DROP PROCEDURE dbo.myStoredProc
GO
This will prevent your error from occurring.

mysql stored procedure call unable to access parameter value

I have a stored procedure named create_new_db(p VARCHAR(45)) on mysqldb. The syntax is as follows.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `create_new_db`( p VARCHAR(45))
BEGIN
CREATE SCHEMA IF NOT EXISTS p DEFAULT CHARACTER SET latin1 ;
-- -----------------------------------------------------
-- Table `p`.`events`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS p.events ( yada, yada, ...)
When I call the stored procedure using the
Call mysqldb.create_new_db('new_db_name');
The procedure executes, the database and tables get created but the parameter i am passing in is NOT being accessed. The database that is created is named p and not the name paramter that i pass in to the procedure
I have tried assigning the paramater to a global variable within the script
#P = p;
I can then see that the parameter name that i am passing into the procedure is getting into the stored procedure but how come it not working ??
I am likely missing something obvious here but ... some help would be appreciated.
You can't create dynamic sql like that. You must use EXECUTE IMMEDIATE 'some dynamic sql stmt'
You pass a string to it, so build your sql up in your stored proc, like this:
EXECUTE IMMEDIATE concat('CREATE SCHEMA IF NOT EXISTS ', p, ' DEFAULT CHARACTER SET latin1');
and similar for your other CREATE statements

Executing MYSQL Stored Procedures using DBVisualizer

I am using DBVisualizer for the first time. I have made a stored procedure in mysql database. However I am unable to execute it from DBVisualizer.
This is how the procedure looks like. Here // is used as delimiter. I have four columns in the table namely slno int (autoincrement), time timestamp, price int, and prodid varchar.
*DROP PROCEDURE `spTest`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `spTest`()
BEGIN
SELECT * FROM dummy1 where prodid=pr01;
END*
In DBVisualizer, I am executing #call spTest()
Where am I going wrong?
As the definer of the stored procedure is root, your DBVizualizer connection to MySQL must be as the root user in order to execute it.