I prepared a statement:
set #table_name = 'W13LAT';
set #sql_text = concat('drop table if exists ',#table_name);
set #sql_text2 = concat('create table ', #table_name, '
(`Student_ID_NUMBER` int(9) unsigned NOT NULL)
ENGINE=MyISAM DEFAULT CHARSET=latin1
');
prepare stmt from #sql_text;
prepare stmt2 from #sql_text2;
execute stmt;
execute stmt2;
The problem is that it doesn't create the table. I'd like to debug by printing out the mysql query generated from the prepared statements. Is this possible right from the mysql command line? For example, I'd like to remove the execute stmt; and execute stmt2; and replace them with echo stmt; or print stmt; or whatever the command is to see the actual statement generated. Also, if you see an error i made as to why it didn't create the table, please let me know!
Thanks!
You're right...it does work. There's this tiny little button at the bottom of Sequel Pro that says 'refresh table list' :/
And thanks for the select #sql_text statement. It worked as well.
Related
I'm trying to create a database named 'uppg' and then use that database once it's created.
The first statement executes as it should, but nothing happens when I try to use the database with the variable. I have tried multiple variations but I don't know why it's not working.
SET #dbname = 'uppg';
SET #q1 = CONCAT(
'CREATE DATABASE `', #dbname, '`');
PREPARE stmt1 FROM #q1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET #q2 = CONCAT(
'USE ', #dbname,
);
PREPARE stmt2 FROM #q2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
There exists complete list of statements allowed in PREPARE: SQL Syntax Permitted in Prepared Statements.
USE is not mentioned in it. So you cannot alter current database using dynamic SQL.
The simplest solution: use complete tables names (including database name) in your queries. In this case the current DB can be any (but not none) - this won't effect your queries.
I am using phpMyAdmin to try to schedule an event that runs every hour. I need it to select data from one of my tables and export it to csv that has a unique name which includes the current timestamp. Outside of an event, I can successfully output from my table to a csv like so:
SET #TimeStamp = DATE_FORMAT(NOW(),'__%Y_%m_%d__%H.%i.%s');
SELECT CONCAT(
'SELECT form_value INTO OUTFILE \'D:/Websites/RTP/contact_form_data/form_data_',
#TimeStamp,
'.csv\' ',
'FROM wp_db7_forms'
) INTO #SQL;
PREPARE stmt from #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
However, I can't for the life of me figure out how to get this working in an event. phpMyAdmin doesn't give very specific error messages, but it seems that the event doesn't like the fact that I am using a variable (it also doesn't seem to like CONCAT). From my research, I've found that this may be because each event executes in a new session, and user-defined variables have session scope.
I tried creating a stored procedure to execute this same block of code, but that experiences similar issues.
Any ideas on an approach I can take to get my code to properly execute in an event?
Try this.
SET #sql =
CONCAT(
'SELECT form_value INTO OUTFILE \'D:/Websites/RTP/contact_form_data/form_data_',
DATE_FORMAT(NOW(),'__%Y_%m_%d__%H.%i.%s'),
'.csv\' ',
'FROM wp_db7_forms'
) ;
PREPARE stmt from #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
The session variable for time is not needed and the session variable #sql must be set as show in the code
This can be saved in mysql 8 and Workbench
DELIMITER $$
CREATE EVENT export_contact_Form_data
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO BEGIN
SET #sql =
CONCAT(
'SELECT form_value INTO OUTFILE \'D:/Websites/RTP/contact_form_data/form_data_',
DATE_FORMAT(NOW(),'__%Y_%m_%d__%H.%i.%s'),
'.csv\' ',
'FROM wp_db7_forms'
) ;
PREPARE stmt from #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
This runs only once in one minute, so that you can i peace check
every thing
and of course the delete query
DROP EVENT IF Exists export_contact_Form_data;
I want to create a SQL script for MySQL 5.7 that inserts data from a table of a database origin into a table of another target database.
I want to have this source-database defined by a variable.
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
INSERT INTO target_table SELECT * FROM #origin_table;
Variables are used in various example to define column names but I never seen a way to define a table with it.
Is anyone has a trick for this ?
Variables won't use in table name in MySQL. You only can use a prepared statement for dynamic build query. For example:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = CONCAT(#origin_db,'.','tablename');
SET #query = CONCAT('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You can read more detail about it in official documentation
You can use Prepared Statement like this:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
SET #qry1 = concat('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt1 from #qry1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
i have table and i want to add column but the name of column will be variable
like this :
$coulName = col_1_2;
ALTER TABLE `table name` ADD `$coulmName` DOUBLE NOT NULL DEFAULT '0' AFTER `col2`;
how can i do that ?
You need to use a prepared statement for this:
SET #colName = 'col_1_2';
SET #s = CONCAT('ALTER TABLE `mytable` ADD `', #colName,
'` DOUBLE NOT NULL DEFAULT 0 AFTER `col2`');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Within MySQL you can only achieve this using prepared statements because you cannot tie a variable to a table or column name. This means that you have to assemble the sql statement in a string and execute it.
However, you can accomplish this from your application code as well - the variable name suggests that you may use php. The same applies: you have to concatenate the sql statement string, cannot use parameters.
Code would look sg like the below in MySQL:
#coulName = 'col_1_2';
#sql = 'ALTER TABLE `table name` ADD `',#coulmName,'` DOUBLE NOT NULL DEFAULT '0' AFTER `col2`;
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
However, I'm not sure if it a really good idea to regularly and dynamically change the existing data structure. That usually indicates poor database design.
I want to know if Mysql server support dynamic table name like this :
SET #tbl = 'Dawin';
INSERT INTO #tbl (col1,col2) VALUES (val1,val2);
because I tried all the possible syntax that I know on my query, but it failed every time.
if it is not supporting such a query, then how to solve this issue?
Thank you
You can try this;)
SET #tbl = 'test';
SET #sql = CONCAT('INSERT INTO ', #tbl, ' (col1,col2) VALUES (val1,val2)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;