Here is a tricky problem i am trying to solve, Having difficulty in solving it
Suppose there is a simple query::
String College="Harvard"
SELECT * FROM College
Above Harvard is the name of the table
College just has the value in it and is not the name of a table
Query will fail because system is assuming College as the name of the table and searching for it which is not there
How to solve this .... in terms of SQL statements
Hope i am clear
Thanks,
How about trying like this :)
SET #College:='Harvard';
SET #sql_text = concat('SELECT * FROM ', #College)
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Related
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;
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 have an old script that creates each day a table and store data in it.
The table names are like this today-date-some-other-string, i.e : 02-02-2018-other-string.
The question is, how could I describe the structure of that table despite I only have today's date? I mean is there a way to do something like this :
DESC WHERE Table like "02-02-2018%"
Thank you.
In mysql you could use prepared statements for example
set #sql = concat('describe ' , (select table_name
from information_schema.tables
where table_name like 'users' and table_schema = 'sandbox')
,';');
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
In Mysql, I am tring to find a way to pass a column value into a variable. Then use the variable as a table name in another query...Below is a MsSQL version of it, Please help me find a Mysql equivalent.
declare #tblname1 varchar(400)
set #tblname1=(SELECT companyname from companies where id=5)
exec(' SELECT sh.streetname FROM '+#tblname1+' sh WHERE sh.id IN (SELECT id from allstreets)')` `
Take a look at the following: https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html
set #tblname1=(SELECT companyname from companies where id=5);
PREPARE stmt1 FROM CONCAT('SELECT sh.streetname FROM ', #tblname1, ' sh WHERE sh.id IN (SELECT id from allstreets)');
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Also, based on your question, I must say that creating a table for each companyname in the company table is a poor design and can only lead to frustration and disappointment.
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.