Mysql Create Table with dynamic database name - mysql

I am creating database at runtime and I want to create the tables in that database at the same time. Can anyone give me any thought on how to do that?
For Example -
I have created one database named 'mydb'
and now in the same process I am trying to create the table I am using the mysql stored procedure for the same.
My proc input will be my dbname. So, my proc looks like
create procedure test(IN dbname varchar(100))
begin
create table `dbname`.`testing`(testid int, testname varchar(45));
end

You can use the PREPARE feature to execute dynamic SQL.

Related

MySQL how to use stored procedures to create database users?

I want to write a stored procedure that allows people to create new database users.
I've looked up on the Internet and it seems that the only answer I find is too complex (MySQL Stored Procedure to create user)
This is what I have, but it is not working. MySQL complains about 'psw'.
create procedure create_user(IN name varchar(50), IN psw varchar(50))
begin
create user name identified by psw;
end
I'd like to know if it is possible to implement the procedure following the logic of the one I presented, and if so, how it is done

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

Mysql Stored Procedure - get rows from multiple database tables

I am trying to get rows from multiple database tables in one of the database stored procedure in mysql.
Is this possible?
Please can you give me example about this.
CREATE DEFINER=`root`#`localhost` PROCEDURE `user_procedure`(IN uid_user int)
BEGIN
select ucgm_id from `conne`.conn_group_membership where uid=uid_user;
END
Cursors can take data from multiple tables, and you can use them in procedures.

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

How to get values returned by stored precedure in MySql + EntityFramewrok

We are using EntityFramework and CodeFirst with MySql Database. In DB I created a stored procedure:
CREATE PROCEDURE `GetDepartmentPath`(leftKey INT, rightKey INT)
BEGIN
.....
END
I tried executing code from here and here(what is very similar to first link), but I always get a NotSupportedExeption. Is there a simple way to execute stored procedures using codefirst and get just a collection of entities?