MySQL 1064 syntax error in stored procedure - mysql

I have stored procedure in local mysql version 5.7.18-log which has this block
but it is not working on AWS RDS 5.7.23-log.
SELECT GROUP_CONCAT( CONCAT(' GROUP_CONCAT(IF(slf.Name = ''', t.NAME, ''' , slfv.Text, NULL)) AS ', t.NAME )) INTO #PivotQuery
FROM (SELECT slf.NAME FROM searchlogfields slf GROUP BY slf.NAME) t;
It throws
syntax error on "SELECT GROUP_CONCAT( CONCAT(' GROUP_CONCAT(IF(slf.Name = ''', t.NAME, ''' , s"
When tried to run this part of the stored procedure in the new query without Begin&End it works.

Related

Error when creating a stored procedure when using XAMPP

Please help me, what's wrong with my query? Why does my query work fine when using Navicat but not when using phpmyadmin from xampp?
I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 7
Please help me, you can check the query below
CREATE DEFINER = `root`#`localhost` PROCEDURE `sp_view_laporan`(
IN `varPName` VARCHAR(255),
IN `varThn` INT(11),
IN `varBln` INT(11)
)
BEGIN
DECLARE
status_pivot LONGTEXT; DECLARE kanwil VARCHAR(255); DECLARE sqlQuery LONGTEXT;
SET
status_pivot =(
SELECT
GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(IF(status_id =',
status_id,
', jml,0)) as ',
LOWER(
REPLACE
(nama_status, ' ', '_')
)
)
)
FROM
(
SELECT
#thn := varThn thn,
#bln := varBln bln
) parm,
view_posisi_status
); IF varPName IS NULL OR varPName = 0 THEN
SET
kanwil = 'WHERE 1 '; ELSE
SET
kanwil = CONCAT(
'WHERE parent_name LIKE "%',
varPName,
'%"'
);
END IF;
SET
sqlQuery = CONCAT(
'SELECT IFNULL(posisi_id,"TOTAL") posisi_id, parent_name, nama_posisi,',
status_pivot,
' FROM (select #thn:=',
varThn,
' thn, #bln:=',
varBln,
' bln) parm,
view_posisi_status ',
kanwil,
'
group by posisi_id WITH ROLLUP'
);
PREPARE
stmt
FROM
sqlQuery;
EXECUTE
stmt;
DEALLOCATE
PREPARE
stmt;
END
I'm pretty sure there's nothing wrong with my code, but xampp says otherwise.
UPDATE :
Tried the suggestion given by FanoFN but still getting the same message
https://www.db-fiddle.com/f/scvxtPrvbfcYzv3EepDyjw/1
UPDATE 2:
I tried to shorten the query, it works with Navicat, but not for xampp.
https://www.db-fiddle.com/f/scvxtPrvbfcYzv3EepDyjw/2

select table and replace column fields by value from another table in mysql

I have been working in ms SQL from the last 3 year, I am new in MySQL, recently we have some MySQL requirement to select a table and replace column fields by value from another table.
I have converted working ms SQL query to MySQL. while executing MySQL query getting an exception. attached table description.here enter image description here
Please advise me on this.
Following are my tried queries
SET #v_Type := 'Account'; // where condition type =account
SET #select := 'SELECT';
SET #columns:= ( SELECT CONCAT('CONVERT(' , `ColDataType` , ', Col' , CONVERT(VARCHAR, `ColNum`) , ') AS [' , `ColName` , '],') FROM `Names` WHERE `TYPE` = #v_Type FOR XML PATH('') )
SET #where:='FROM `Data` WHERE [Type] = ''' + #v_Type + ''''
SET #statement = CONCAT(#select,#columns,#where);
PREPARE myStatement FROM #statement;
EXECUTE myStatement;
While executing I got the error like
Query: set #columns:= ( SELECT CONCAT('CONVERT(' , ColDataType , ', Col' , CONVERT(VARCHAR, ColNum) , ') AS [' , ColName , '],') ...
Error Code: 1064
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 'VARCHAR, ColNum) , ') AS [' , ColName , '],') FROM `Name' at line 2
Thanks in advance.
SELECT CONCAT( 'SELECT ', GROUP_CONCAT(CONCAT('col', colnum, ' ', colname)),
' FROM data',
' WHERE type = ?')
INTO #sql
FROM names
WHERE type = #v_type
GROUP BY type;
PREPARE myStatement FROM #sql;
EXECUTE myStatement USING #v_type;
DROP PREPARE myStatement;
fiddle

Convert dynamic SQL Server Pivot to MySQL

I have written below stored procedure in sql server
CREATE PROCEDURE usp_getQuantityBilling()
-- VARIABLE --
DECLARE #cols AS NVARCHAR(MAX)
DECLARE #query AS NVARCHAR(MAX)
select #cols = group_concat(name)
from visit_groups where arm_id='377';
set #query = 'SELECT id, service, rate, subjectcount, armDetailsId, totalHours,'
+ #cols + ' FROM
(
select protocols.id, arms.name, visit_groups.name as visitGroup, services.name as serviceName, visits.quantity,
line_items_visits.subject_count
from protocols,arms,visit_groups,visits,line_items,line_items_visits,services
where protocols.id = arms.protocol_id
and visit_groups.arm_id = arms.id
and visits.visit_group_id = visit_groups.id
and line_items_visits.arm_id = arms.id
and visits.line_items_visit_id = line_items_visits.id
and line_items_visits.line_item_id = line_items.id
and services.id = line_items.service_id
and protocols.id = ''' + 591 + '''
) x
pivot
(
max(isActive)
for visitName in (' + #cols + ')
) p';
execute (#query)
I am trying to convert this into mysql server but I am not sure how to declare the variables in mysql stored proc and how to run all these quesry together where result generated by previous query can be used in next query.
Also below query:
select #cols = group_concat(name)
from visit_groups where arm_id='377';
is giving error
Error Code: 1267. Illegal mix of collations (utf8_general_ci,IMPLICIT)
and (utf8_unicode_ci,IMPLICIT) for operation '=' 0.000 sec
so i looked for the solution and made it like this
select #cols = group_concat(name) COLLATE utf8_unicode_ci from visit_groups
where arm_id='377';
Now I am getting result as 0 with a column name #cols = group_concat(name) COLLATE utf8_unicode_ci , but the result should actually look like visit1, visit 2 because that is what I am getting in sql server.
Edit
SELECT id, name, visitGroup, serviceName, quantity, subject_count,'
+ [Visit 1],[Visit 2] + ' FROM
(
select protocols.id, arms.name, visit_groups.name as visitGroup, services.name as serviceName,
visits.quantity,
line_items_visits.subject_count
from protocols,arms,visit_groups,visits,line_items,line_items_visits,services
where protocols.id = arms.protocol_id
and visit_groups.arm_id = arms.id
and visits.visit_group_id = visit_groups.id
and line_items_visits.arm_id = arms.id
and visits.line_items_visit_id = line_items_visits.id
and line_items_visits.line_item_id = line_items.id
and services.id = line_items.service_id
and protocols.id = ''' + 591 + '''
) x
pivot
(
max(isActive)
for visitName in (' + [Visit 1],[Visit 2] + ')
) p'
main issue is running this query, it shows error in mysql that pivot is used at wrong position but in sql it works fine. Anyway to turn this query into mysql pivot query.
Note: this Visit 1 and Visit 2 I am generating dynamically but just for this solution I copied here manually.

Mysql with parameters not working

I have a mysql stored procedure like this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `accounts_summary_by`(
IN **created_by** int(10)
)
BEGIN
SET group_concat_max_len=2048;
SET #sql = NULL;
SELECT
GROUP_CONCAT(
DISTINCT CONCAT(
'MAX(IF(fiscal_year = ''',
fiscal_year,
''', amount, 0)) AS ',
CONCAT("'",fiscal_year,"'")
)
) INTO #sql
FROM
net_savings;
SET #sql = CONCAT('SELECT accounts.id,
accounts.account,
accounts.region,
accounts.cluster,
accounts.industry, ,'
,#sql,
'FROM net_savings join accounts
on accounts.id = net_savings.account_id
Where accounts.user_id = **created_by**
GROUP BY accounts.account,net_savings.account_id
ORDER BY accounts.id');
PREPARE statement FROM #sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END
But upon calling the procedure like these:
CALL accounts_summary_by(2)
2 is a user_id reference to another table called users.
It gave me an error. Please help as I can't find any fixed to my problem.
0 72 23:41:12 CALL `buckets`.`accounts_summary_by`(1) Error Code: 1054. Unknown column 'created_by' in 'where clause' 0.000 sec
MySQL's internal programming language is not php, it is not going to resolve variables within a text, so you need to concat it properly to the middle of the prepared statement:
SET #sql = CONCAT('SELECT accounts.id,
accounts.account,
accounts.region,
accounts.cluster,
accounts.industry,'
,#sql,
'FROM net_savings join accounts
on accounts.id = net_savings.account_id
Where accounts.user_id ='
,created_by
,'GROUP BY accounts.account,net_savings.account_id
ORDER BY accounts.id');
Since created_by is a parameter of the procedure, you do not need to preposition it with #.

MySQL: how to include '\G' inside a procedure

I am creating a procedure in MySQL with the following codes:
delimiter #
drop procedure if exists a03_strings #
create procedure a03_strings ()
begin
select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client
, group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals
from vt_clients
left join vt_animals using (cl_id)
group by cl_id\G;
end;
#
I added \G at line 9 in order to get the following output:
So the output is not a table but rows.
When I test this just as select statements it worked with \G. However, when I include \G inside the procedure and it is giving me error saying that I cannot include a procedure inside another procedure.
Is there an alternative to achieve the output I need?
Thanks!
Short answer is no: '\G' is handled by the mysql client not the DBMS (where the stored procedure runs. But you can simply:
mysql> create procedure a03_strings ()
-> begin
-> select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client
-> , group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals
-> from vt_clients
-> left join vt_animals using (cl_id)
-> group by cl_id\G;
-> end;
-> #
...
mysql-> call a03strings() \G#
Or use this as your SELECT...
SELECT IF (rowselect='A',
CONCAT('Client: ', ilv.Client),
CONCAT('Animals', ilv.Animals)
FROM (
select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client
, group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals
from vt_clients
left join vt_animals using (cl_id)
group by cl_id
) ilv,
(SELECT 'A' AS rowselect UNION SELECT 'B') AS rowmultiplier