Using a variable in the FROM clause - mysql

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.

Related

Get specifi field value from a dynamic SQL in MYSQL

I am trying to create and execute sql dynamically in MYSQL. I have a field myColumn and i want to show/get the value of this field. The below script will get me all data in the table,
SET #value:='myColumn';
SET #sql:=CONCAT("SELECT * FROM change c where c.field='",#value"'");
PREPARE dynamic_statement FROM #sql;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
The reason i want to do this is because I want to work on that specific field. Like execute a function on the field for e.g. repalce,substring etc etc. I know i can do any work inside #sql but i want to know if i can access the columns once the sql is executed.
select #sql.myColumn will not work. How can i achieve this?

Use user-defined variable in Create Table statement to specify varchar length

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;

How to write generic code for union tables over years?

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;

Why is my PREPAREed statment giving the same value sha1 for all rows?

Dipping pinky into prepared sql.
MySQL 5.6.23 InnoDB
So far I have this
PREPARE stmt1 FROM #stmt;
select #stmt, #key1, #feed_id;
-- result is like this
-- stmt = "select sha1(concat('{',? ,'}',?)) sha1, id from mytable"
-- key1 = "'location=',Location,'term=',Term"
-- feed_id = 10
EXECUTE stmt1 USING #key1, #feed_id;
This gives me a result BUT the first column (sha1) is the same value for each result row. That is not correct!
If I do the substitution myself and run the command I get what I expected (a different value for sha1 for each row)
This is an example statement that works as I expect
select sha1(concat('{','location=',Location,'term=',Term ,'}',1129)) sha1,
id
from My_table
Makes me think about volatile functions But that is not a thing in MySQL or is it?
I am using dynamic SQL because the key1 and the table names can both be calculated and I have many thousands to process.
anyone have an idea what is going on or some things to try?
I solved this problem by creating my statement string with all the values and then issuing a PREPARE then EXECUTE did not require any params. It worked. But I will need to RE PREPARE the statement for each execution. That is sloppy but good enough for my needs.
The statement now looks like this:
SET #stmt2 = CONCAT("select sha1(concat('{',", #key1," ,'}',", #feed_id, ")) sha1, ts_id from ",#table_name);
PREPARE stmt3 FROM #stmt2;
EXECUTE stmt3;

solving a Simple SQL query

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;