Troubleshooting on Dynamic SQL in MySQL
Hello,
I am having trouble on running below Dynamic SQL query. Can someone have a look and troubleshoot what am I doing incorrectly? Also, is there any way I can able to print Dynamic SQL query?
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(case when x.question_text = ''',
question_text,
''' then x.answer_text end) AS ',
question_text, '`'
)
) INTO @sql
from
(SELECT DISTINCT aa.application_id,aq.question_text,aa.answer_text
from tablea aq
inner join tableb aa on aa.application_question_id = aq.id
where aq.program_id = 136)x;
SET @sql
= CONCAT('SELECT aa.application_id, ', @sql, '
from tablea aq
inner join tableb aa on aa.application_question_id = aq.id
group by aa.application_id;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Answers
-
This is the error I am getting:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Manufacturing - Does your business either enable or stand to benefit from the ut' at line 1
0 -
What is the error message or unexpected behavior you are seeing?
0 -
I'd recommend breaking your SQL down into chunks.
Start by looking at what's being returned from your first query:
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'sum(case when x.question_text = ''', question_text, ''' then x.answer_text end) AS ', question_text, '`' ) ) from (SELECT DISTINCT aa.application_id,aq.question_text,aa.answer_text from tablea aq inner join tableb aa on aa.application_question_id = aq.id where aq.program_id = 136)x;
Also looking at this you have an unmatched back tick (`) which may cause some issues. Also, are you certain you want SUM here and not COUNT since SUM is for numerical values but it appears you're wanting to count the number of answers in the answer_text column.
The error you're receiving appears to be saying that you're attempting to utilize some text as actual SQL which it doesn't like. Make sure your string is quoted properly.
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1 -
What is the error message or unexpected behavior you are seeing?
0 -
@GrantSmith, thanks for your response. I did able to run query successfully but only thing is that I can't able to display whole question_text_orig as column header. With using below query, it running fine using displaying first 5 characters as question_text.
I have tried using group_concat_max_len as well but its still giving me below error.
Error Message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Manufacturing - Does your business either enable or stand to benefit from the ut' at line 1
SQL Query:
SET session group_concat_max_len=9999999999;
DROP TEMPORARY TABLE tmp_application_data;
CREATE TEMPORARY TABLE tmp_application_data
SELECT DISTINCT aa.application_id,aq.question_text as question_text_orig,left(aq.question_text,5) as question_text,aa.answer_text
from table1 aq
inner join table2 aa on aa.application_question_id = aq.id
where aq.program_id = 136;
-- select * from tmp_application_data;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when question_text = ''',
question_text,
''' then answer_text end) ',
question_text
)
) INTO @sql
from
tmp_application_data;
SET @sql = CONCAT('SELECT application_id, ', @sql, '
FROM tmp_application_data
GROUP BY application_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
0 -
@jackb1117, this is the error message I am getting.
Error Message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Manufacturing - Does your business either enable or stand to benefit from the ut' at line 1
Please refer my previous comment as progress on discussion topic.
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 754 Beast Mode
- 61 App Studio
- 41 Variables
- 693 Automate
- 178 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive