I want to be able to do something like this:
SET #foo_width = 10;
CREATE TABLE test_table (
foo varchar(#foo_width)
);
The exact problem is that I will need the same varchar-width for different columns across different tables. So, thought it'd be better if it could be stored in a variable.
Is there any alternative?
You can't do it directly, and MySQL doesn't support user-defined types.
At best, you can use PREPARED STATEMENT like this or you generate your query in your application.
SET #foo_width = 10;
SELECT CONCAT("CREATE TABLE test_table (
foo varchar(",#foo_width,")
);") INTO #myquery;
select #myquery; -- only for test
PREPARE stmt FROM #myquery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Related
create table a.a {
db varchar(255)
}
//first record of a.a is db = b
create table b.a {
id varchar(255)
}
mysql to run:
use concat((select * from a.a limit 1),".a"); select * from a;
How can i achieve the above? Using the returned results of one table to access another database without doing round trips to mysql connection.
Dynamic SQL requires using PREPARE in a stored procedure.
SET #sql = (SELECT CONCAT('SELECT * FROM `', a.a, '`')
FROM a
LIMIT 1);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
It's generally considered best to avoid designing your database so that it requires this. Don't spread the data among multiple tables with the same structure, use a single table and add another column to distinguish the records.
I have an Hive ETL job where I have to extract data from yearly tables and union them. Don't ask why there is a separate table for each year (legacy systems and huge size).
Lets assume table names are table11, table12, . . ., table19
Now I can write query upto 'from' table19, but I want to write generic code, otherwise the code have to be updated every year. I believe one can't use wildcards in the 'from' clause, if I am correct. e.g. table20*
Best Regards,
you can use prepared statement. So you can generate a query with CONCAT and then execute it.
-- SELECT CONCAT("insert into newtable select * from table",DATE_FORMAT(now(),'%y'))
SELECT CONCAT(" select * from mysql.user") INTO #sql;
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I am trying add one column in my Mysql database that sums all the columns starting by 'tokenvalid' which can take the value of 1 or 0.
And let's say I have 50 columns like that in my database (i.e. tokenvalid1, tokenvalid2 ...., tokenvalide50) with other columns between.
Please find below the code I would like to implement. I know that is not correct at all but it is just to give you an idea of what I am trying to do.
Thank you for your help!
'SELECT *, sum(column_name LIKE "tokenvalid"%) as total FROM points WHERE 1'
This post should help you. The post describes how to get the columns and then query for results.
MySQL Like statement in SELECT column_name
Something like this should help you.
SET #colname = (SELECT GROUP_CONCAT(`column_name`) from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='points' AND `column_name` LIKE 'tokenvalid%');
SET #table = 'points';
SET #query = CONCAT('SELECT SUM(',#colname,') FROM ', #table);
PREPARE stmt FROM #query;
EXECUTE stmt;
Similar to this answer by RocketDonkey
If the string is in your external application (like PHP), sure, just construct the MySQL statement.
If the string is inside a MySQL table, you can't. MySQL has no eval() or such function. The following is impossible:
Suppose you have a table 'queries' with a field "columnname" that refers to one of the column names in the table "mytable". There might be additional columns in 'queries' that allow you to select the columnname you want...
INSERT INTO queries (columname) VALUES ("name")
SELECT (select columnname from queries) from mytable
You can however work with PREPARED STATEMENTS. Be aware this is very hacky.
SELECT columnname from queries into #colname;
SET #table = 'mytable';
SET #s = CONCAT('SELECT ',#colname,' FROM ', #table);
PREPARE stmt FROM #s;
EXECUTE stmt;
I try to use the following code to empty a database (delete all tables) :
SELECT concat('DROP TABLE IF EXISTS ','`',table_schema,'`','.','`',table_name,'`',';') FROM information_schema.tables WHERE table_schema = 'DB';
I am getting an output of the commands, but nothing happens to the database. If I take an individual command from the output, and run it in the console, it works.
What am I doing wrong?
As you know, SELECT only returns the result of a query. It doesn't know you actually intend to execute the result of that query. (In most cases, that would make no sense.) You can use prepared statements to do what you want (untested):
SET #s:='';
SELECT #s:=concat(#s, 'DROP TABLE IF EXISTS ','`',table_schema,'`','.','`',table_name,'`',';') FROM information_schema.tables WHERE table_schema = 'DB';
PREPARE stm FROM #s;
EXECUTE stm;
DEALLOCATE PREPARE stm;
Why you're using SELECT statement, Simply try this query:
DROP TABLE IF EXISTS table_name;
I need to choose a table according to an instruction. But if I use a variable to store the name of the table, MySQL returns error 1064.
SET #eligetabla ='convenios';
select * from #eligetabla;
How can I use a variable for a table following the from clause?
Not that I think this is a particularly fantastic idea, and its quite the pain, but you can do this:
SET #eligetabla='convenios';
SET #sql=CONCAT("SELECT * FROM ", #eligetabla);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You may as well be choosing the table application side and building your query there, tho.