MySQL Variable Database Name Syntax Error - mysql

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;

Related

mysql stored procedure multiple parameters

I am trying to add multiple dynamic columns in the select statement, (stored procedure) But it's not working at my end, Can you please help me on same.
BEGIN SET #col1 = col1;SET #col1 = col2;SET #getID = CONCAT('SELECT ' ,#col1 = col1, #col2 = col2,' FROM',tablename_In,' WHERE `type`= type ORDER BY id DESC LIMIT ?,?'); PREPARE stmt FROM #getID;SET #START =_START; SET #LIMIT = _LIMIT;EXECUTE stmt USING #START, #LIMIT; DEALLOCATE PREPARE stmt; END
Also, I have another stored procedure it's also not working with between query:
BEGIN SET #getID = CONCAT('SELECT count(id) as co FROM ',tablename_In,' WHERE',est_time,' BETWEEN',start_date,'AND',end_date);PREPARE stmt FROM #getID; EXECUTE stmt; DEALLOCATE PREPARE stmt; END

how to get the current value for any column in my sql

I have a procedure where i need to check the current value every time in the another query
SET #sql = NULL;
set #ct=(select count(alloc_hrs) from emp_sample where week(alloc_date)=<current_value>);
SET SESSION group_concat_max_len = 1000000;
SELECT GROUP_CONCAT(DISTINCT
(CONCAT ('MAX(CASE WHEN alloc_date=''',
alloc_date,
''' THEN #ct END) `',
year(alloc_date),week(alloc_date),
'week`'
))
)
INTO #sql
FROM emp_sample;
SET #sql = CONCAT('SELECT emp_code, ', #sql,' FROM emp_sample group by emp_code');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Is there any function like this() in mysql
If not what should I replace to get that

select list of columns dynamically in MySQL

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.

pivot query doesn't execute on mysql

in reference to this [pivot article]
I managed to get this prepared pivot query
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS ',
name
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
and here's a live demo SQLfiddle
the problem is when I try to execute that on my data with the same schema using mysql(5.6.14 Win32 x86) I get an error.
Error
SQL query:
PREPARE stmt FROM #sql ;
MySQL said:
#1064 -
just an error code but no message..
I've read SO questions like this & this but the answers are the static way which won't work with unknown columns
first of all, is this even available in mysql?? .. any pointers are appreciated
Your schema may be the same, but your data is probably different. You may have a keyword, space, or something else in your values which is causing the issue. Try to wrap your alias in double quotes.
Here's my edit to your SQLFiddle. I added a line which outputs the dynamic SQL in case you need to examine what you get on your system with your data.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(IF(name = ''',
name,
''', 1, NULL)) AS "',
name, '"'
)
) INTO #sql
FROM bundles;
SET #sql = CONCAT('SELECT ', #sql, ' FROM bundles');
SELECT CONCAT(#sql);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Paramater for name of a table without using prepare statement in MySQL

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;