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
Related
I'm trying out MySQL procedures for the first time, however I can't figure out how to define the variable #index_ids for the life of me. It really doesn't like the SET.
CREATE PROCEDURE #indextemp
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END
Problem is in CREATE PROCEDURE syntax, not in setting variable. You just have to add parentheses after procedure name. Here's working sample
delimiter $
CREATE PROCEDURE indextemp()
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END$
delimiter ;
Sometimes use of delimiter character in procedure body can cause problems too. That's why I set delimiter to $ before creating procedure and revert it to default ; after I'm done.
Also notice that I have removed # from your procedure name. In sql # is used to insert comments. If for some reason you really want to use it in your name you have to do it like that
CREATE PROCEDURE `#indextemp`()
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
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.
I am learning to write MySQL stored procedures and I have encountered some difficulties. Here I have two stored procedures:
First stored procedure:
CREATE PROCEDURE sp1 (IN `username` TEXT, OUT `user_id` INT)
BEGIN
DECLARE rowcount INT;
SELECT count(`User ID`) INTO rowcount FROM user WHERE `Username`=username;
SET user_id = rowcount;
END|
Second stored procedure:
CREATE PROCEDURE sp2 (IN `doc_id` INT, IN `content` LONGTEXT)
BEGIN
UPDATE doc SET `Content`=content WHERE `Doc ID`=doc_id;
END|
(Delimiter is |.)
Question:
I observe that the result of the first stored procedure is the same as calling SELECT count(`User ID`) FROM user;. However, the second stored procedure does its job and gets the content updated with the new content.
So why does the first stored procedure treat `Username` and username as equal identifiers but the second stored procedure treats `Content` and content as different identifiers? The two identifiers in both cases are the same except the capitalization of the first letter.
I figure it out after reading the official MySQL documentation about the scope of local variables.
It states that:
A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.
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.