I've looked through a lot of posts on this site and others and I can't figure this out. I'm trying to select a list of columns from a table and then use them in a query similar to this:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #s = 'select ' + #cols + ' from REPORTS.t_labor';
prepare stmt from #s;
execute stmt;
deallocate prepare stmt;
-- execute ('select ' + #cols + ' from REPORTS.t_sales');
I am trying to run the above statements as-is. I also tried creating a stored procedure, but I have never worked with stored procedures in MySQL before and I don't know how to debug them. I would prefer to do this without using a stored procedure if possible, but if it's necessary that's ok.
I tried using the prepare statement, and I tried the execute statement but I couldn't get either to work.
Wild guess here, but try this.
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
PREPARE stmt1 FROM 'select ? from REPORTS.t_labor';
EXECUTE stmt1 USING #cols;
DEALLOCATE PREPARE stmt1;
Finally figured it out:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #qry = concat('select ', #cols, ' from REPORTS.t_labor');
PREPARE stmt1 FROM #qry;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Had to piece together replies from at least 5 different posts similar to this question plus Dr OSWaldo's reply.
Related
I'm getting all columns from a table and fetching the result to JSON using the function JSON_OBJECT. But when i execute the stored procedure i'm getting this error.
CREATE DEFINER=`sistema`#`%` PROCEDURE `get_products_as_json`()
BEGIN
SET #fields = (
'SELECT
group_concat(\'\`\', column_name, \'\`\, \', column_name)
FROM
information_schema.columns
WHERE
table_schema = DATABASE()
AND table_name = \'products\'
ORDER BY table_name , ordinal_position'
);
SET #stmt = ('SELECT JSON_OBJECT(?) FROM products LIMIT 10');
PREPARE stmt FROM #stmt;
EXECUTE stmt USING #fields;
DEALLOCATE PREPARE stmt;
END
You cannot use prepared statements in that way.
You have to CONCAT the variable.
I also changed the first SELECT, this works too and is better to read.
DELIMITER $$
CREATE DEFINER=`sistema`#`%` PROCEDURE `get_products_as_json`()
BEGIN
SET #fields = (
SELECT
group_concat('"', column_name, '", ', column_name)
FROM
information_schema.columns
WHERE
table_schema = DATABASE()
AND table_name = 'products'
ORDER BY table_name , ordinal_position
);
SET #stmt = CONCAT('SELECT json_object(',#fields,') FROM products LIMIT 10');
PREPARE stmt FROM #stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
I added also the DELIMITER in case you don_'t use mysql workbench
I'm trying to set a database name for a MySQL query. I've been able to get my first query to work, but the second I'm struggling to figure out where my syntax error is. I'm guessing it has something to do with the variable, but I'm stuck.
SET #db = CONCAT('SELECT client_databases.database_name
FROM client_databases
JOIN jobs ON jobs.organisation_id = client_databases.organisation_id
WHERE jobs.transaction_reference="K01-REC0000001"');
SET #q = CONCAT('SELECT receipts_lines.id, product_code, product_name, receipts_lines.is_putaway
FROM ', #db ,'.receipts
JOIN ', #db ,'.receipts_lines ON receipts_lines.receipt_id = receipts.id
WHERE reference_number="K01-REC0000001"');
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You should do something like this. Where you select into #db the databasename and use this to build your stmt
SELECT client_databases.database_name INTO #db
FROM client_databases
JOIN jobs ON jobs.organisation_id = client_databases.organisation_id
WHERE jobs.transaction_reference="K01-REC0000001";
SET #q = CONCAT('SELECT receipts_lines.id, product_code, product_name, receipts_lines.is_putaway
FROM ', #db ,'.receipts
JOIN ', #db ,'.receipts_lines ON receipts_lines.receipt_id = receipts.id
WHERE reference_number="K01-REC0000001"');
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
To do point, I have simple query like
SELECT * FROM mytable WHERE concat(firstName, ' ', lastName) in ('Adan Jack');
Query above run smoothly. But how if I combine that condition using Dynamic Query that using concat before?
I did this:
BEGIN
set #cond = concat(concat("firstName"," ", "lastName"), " in ('Adan Jack')";
set #query = concat("SELECT * FROM mytable WHERE ", #cond);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
But no result and cause error.
Thanks.
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;
can i somehow set parameter for name of table in query without using a prepare statement?
This is example:
SET #tableName = 'Customer';
SELECT * FROM #tableName;
Thanks
Depending on the version of MySQL you are using, you may be able to use something like:
SET #tableName = 'Customer';
SET #s = CONCAT('SELECT * FROM ', #tableName);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;