First I tried this, (MySQL/phpmyadmin)
CREATE DEFINER=`root`#`localhost` PROCEDURE `tempcheck`() NO SQL
BEGIN
SET #query_string = 'SELECT * FROM properties';
PREPARE query_statement FROM #query_string;
EXECUTE query_statement;
DEALLOCATE PREPARE query_statement;
END
This just fetches the first record of the table 'properties'. The table has more than one value. When I sqlquery 'select * from properties;' it returns the whole table.
I even tried this simple method
BEGIN
SELECT * FROM properties';
END
Tried many stackoverflow Q&As. They all suggest that I make a temp table. Even if I do, how will it return the whole table when it doesn't return the entire table here in the first place. CAN a mysql stored procedure actually return a whole table or NOT. if it can, then how?
Guys thank you all for your help. One of my colleagues helped me fix it. Apparently it's a bug in phpmyadmin. phpmyadmin won't return more than one record
Related
set #sourcedb = 'testdb1' set #destinationdb = 'testdb2' CREATE TABLE IF NOT EXISTS #destinationdb.Testtable Like #sourcedb.Testtable;
I have tried copy one table from one database to another database with same name.
how to pass database name as a parameter?
I've tried that before but id doesn't work. I wonder if there's a way to that way though. In the meantime I'm just using prepared statement instead. See example query below:
SET #ctable='CREATE TABLE testdb12.testtable LIKE testdb21.testtable';
PREPARE stmt FROM #ctable;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Now, I don't really know your usage but in my case, I use this to create a new table on the 1st every month. So I keep this query in a text file, use a batch script to call it and then use task schedule to execute it every 1st of the month (yes, I'm using Windows). Hopefully, this might be what you're looking for.
*I found this solution in one of the answer here but I can't remember/didn't bookmark the solution. I would like to credit this properly.
Edit : I found this mysql create table dynamically .. maybe you can use it
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).
i want to create a table and append NOW() to the end of the table name.
what would be the syntax please?
oops thank you for the correction.
My reason why i wat to do this is i have a script that runs on the database table everyday. If certain conditions are met i delete from the master table. BUT before i delete i just want to make a backup copy by running: create table rc_profile_backup_table_NOW() like rc_profile_table;
then i run: insert rc_profile_table_backup_NOW() select * from rc_profile_table
hope this is better.
thanks
Here is some rather ugly way to do it, using prepared statements. You might adjust the "now" part, because now makes invalid tables names I think.
set #c=concat('create table zogi_',date_format(now(),'%Y_%m_%d'),' (a varchar(10))');
prepare stmt from #c;
execute stmt;
deallocate prepare stmt
describe zogi_2012_02_07;
It creates a table named zogi_[year][month][day].
I am trying to write a simple stored procedure which will clear the contents of some tables in the current database - I'm doing this by matching a prefix against the list of tables in the information_schema:
delimiter $$
create procedure local_flush_cache(db varchar(255))
begin
select #str_sql:=concat('delete from ',group_concat(table_name))
from information_schema.tables
where table_schema=db and table_name like 'cache_%';
prepare stmt from #str_sql;
execute stmt;
drop prepare stmt;
end$$
delimiter ;
I would like to be able to remove the db parameter and have the function work on the currently active database.
I guess I will also need the ability to recognise that a database has not yet been selected and output an error (to prevent accidentally flushing all cache tables in all databases).
Considering that the procedure is tied to a database, the current is the called unless you do CALL db.proc().
However, if you really want the selected one, you can the DATABASE() function:
where table_schema=database() and table_name like 'cache_%';
I have just converted a SQL select statement into a stored procedure
The SQL Statement use select statement takes
0.4784s to run the first time and 0.0003s after that
The Stored procedure takes 0.4784s to run every time.
I assume the query cache is not been used
How can I rectify this?
A simplified version of the code
SELECT * FROM Venues WHERE VenueName = :TheVenue
=======
CREATE PROCEDURE GetVenues
(
TheVenue VarChar(22)
)
BEGIN
SELECT * FROM Venues WHERE VenueName = TheVenue
END;
Welcome to MySQL... it is really difficult to get anything within a stored procedure to take advantage of the query cache. The dev article A Practical Look at the MySQL Query Cache discusses this in some detail. The limitations are also mentioned in the reference documentation here and on the MySQL Performance Blog.
Basically, don't depend on caching of queries executed inside of stored procedures. It is near impossible to get it to work though the first reference does claim that it is possible. This usually isn't a problem if you are using stored procedures to encapsulate complicated logic. Most of the problems that I have seen were caused by using stored procedures for very simple queries where a VIEW would have sufficed.
You could try a dynamic SQL stored procedure, like:
CREATE PROCEDURE GetVenues (TheVenue varchar(22))
BEGIN
SET #s = 'SELECT * FROM Venues WHERE VenueName = ?';
SET #v = TheVenue;
PREPARE stmt1 FROM #s;
EXECUTE stmt1 USING #v;
DEALLOCATE PREPARE stmt1;
END;
No MySQL server by hand to test the syntax, so you might have to tweak it.