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

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)

Related

How to pass table type parameter in store Procedure in MySQL like MS-SQL do?

Can i pass table valued parameter in store procedure in MY-SQL like MS-SQL.
For sql procedure below:
CREATE PROCEDURE dbo. usp_InsertProductionLocation
#TVP LocationTableType READONLY
AS
SET NOCOUNT ON
INSERT INTO AdventureWorks2012.Production.Location
(Name
,CostRate
,Availability
,ModifiedDate)
SELECT *, 0, GETDATE()
FROM #TVP;
GO
For MySQL what should i do?

How to handle one Stored Procedure output (ResultSet or Table) into another Stored Procedure as Table

one Stored Procedure is returning me a table and I want to hold that result into another Stored Procedure.
and I am facing SQL syntax error.
First Stored Procedure
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnTable`()
BEGIN
SELECT `user_id`, `email` FROM `users`;
END$$
DELIMITER ;
and I am trying to call this Store Procedure into another and want to hold the data into a view or table
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnCall`()
BEGIN
DROP VIEW IF EXISTS `my_view`;
CREATE OR REPLACE VIEW my_view AS (CALL testReturnTable());
SELECT * FROM my_view;
END$$
DELIMITER ;
and getting error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CALL testReturnTable());
I can use function here as well, but I don't know how to handle result set,
please advice.
it is not possible to get table ouput from one stored procedure to another stored procedure. But we have alternate to do this which is Temp table

MySQl IF NOT EXISTS, CREATE Stored procedure

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.

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.