Get specifi field value from a dynamic SQL in MYSQL - 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?

Related

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;

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;

SQL - Select rows that contain a NULL value in a non-nullable column

I'm hoping somebody can help me with a script / query, the target DB is mySQL.
The database I am working with does not conform to it's own constraints and is in the process of being moved to MS SQL. What I am looking to find is a query that can be run against a table which looks for rows that contain a null value in a column that does not allow nulls, which in turn will assist with SSIS DFT debugging times.
Many thanks.
Try:
SELECT group_concat(`COLUMN_NAME`) as myList
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename'
-- AND `IS_NULLABLE`='NO'
into #colname;
SET #query = CONCAT('SELECT ',#colname,' FROM yourtablename');
PREPARE stmt FROM #query;
EXECUTE stmt;

Using a variable in the FROM clause

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.

MySQL raw query: using #var

I'm trying to rename a list of tables using a constant prefix defined as a #var:
SET #p='newprefix_';
RENAME TABLE `oldprefix_tablename1` TO CONCAT(#p, 'tablename1');
RENAME TABLE `oldprefix_tablename2` TO CONCAT(#p, 'tablename2');
This syntax is wrong, but I see that:
SELECT CONCAT(#p, 'tablename'); //outputs newprefix_tablename
What's the correct way/syntax to use here?
You can't do it directly the way you are trying. As the manual says (http://dev.mysql.com/doc/refman/5.1/en/user-variables.html)
User variables are intended to provide data values. They cannot be
used directly in an SQL statement as an identifier or as part of an
identifier, such as in contexts where a table or database name is
expected, or as a reserved word such as SELECT.
You have to use prepared statements:
SET #p = 'newprefix_';
SET #s = CONCAT('RENAME TABLE `oldprefix_tablename1` to ', #p, 'tablename1');
PREPARE stmt FROM #s;
EXECUTE stmt;