Error when creating a stored procedure when using XAMPP - mysql

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

Related

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

Cannot use prepare statement on locally declared variable

In a procedure I want to select a value from a specific table in all databases. I developed a code and reached to this point:
BEGIN
DECLARE dynamic_query LONGTEXT;
SET SESSION group_concat_max_len = 100000000;
SET dynamic_query = (
SELECT
GROUP_CONCAT(
"SELECT option_name AS _key, option_value as _value from ",
TABLE_NAME,
" where option_name like '",
"something%'" SEPARATOR ' union ')
FROM
(
SELECT
CONCAT(`database_name`,'.',`table_prefix`,'options') as TABLE_NAME
FROM `tablename`
WHERE `database_name` <> '' AND `table_prefix` <> ''
) AS tmp
);
SELECT dynamic_query;
END
And I copy the output and execute it and it works fine. But when I add a prepare statement like below I get an error which is mentioned in this bug.
PREPARE result_query FROM dynamic_query;
EXECUTE result_query;
DEALLOCATE PREPARE result_query;
Is there any other way so I can get my desired output?
P.S.: I'm using SET SESSION group_concat_max_len = 100000000; to expand group_concat's limit but I prefer another way because the number of databases are growing.

SQL syntax error using prepared statements and variables

I have three stored procedures for three different ERPs making almost exactly the same operations, so I decided to make just one, and use parameters. Some of the field names change depending on the parameters, so I set them in variables. Then I am using prepared statements to make it work.
A working update that does not use prepared statements is as follows:
update lt_erp_barcodes
set barcode_list = concat(',', curr_barcode, barcode_list)
where cod_navision = curr_erp_code;
It updates the current barcode_list adding curr_barcode at the beggining of the list, and adding also a comma. (,curr_barcode,the rest of the list,)
My approach to make it work with prepared statement is:
set #erp_code_field_name = "cod_navision";
set #curr_erp_code = '12345';
set #curr_barcode = '123123123';
set #q1 = concat('update lt_erp_barcodes
set barcode_list = (\',\'', #curr_barcode, ' barcode_list)
where ', #erp_code_field_name, ' = ', #curr_erp_code);
prepare update_barcode_list from #q1;
execute update_barcode_list;
But when I call to prepare the following error is raised:
0 228 10:30:17 prepare update_barcode_list from #q1 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 '123123123 barcode_list
where cod_navision = 12345' at line 2 0.047 sec
How could I make the second block of code work the same as the first one?
Thanks
Your code is generating non legal SQL in the #Q1 variable:
DELIMITER $
DROP PROCEDURE IF EXISTS procTest$
CREATE PROCEDURE procTest()
BEGIN
set #erp_code_field_name := "cod_navision";
set #curr_erp_code := '12345';
set #curr_barcode := '123123123';
set #q1 := concat('update lt_erp_barcodes
set barcode_list = (\',\'', #curr_barcode, ' barcode_list)
where ', #erp_code_field_name, ' = ', #curr_erp_code);
SELECT #q1;
prepare update_barcode_list from #q1;
execute update_barcode_list;
END$
DELIMITER ;
CALL procTest();
See the result here:
update lt_erp_barcodes
set barcode_list = (','123123123 barcode_list)
where cod_navision = 12345
What is the actual result you are trying to achieve? Let me know if you still can't solve this,
James
You may need to take off the ' barcode_list' from your #q1
set #q1 = concat('update lt_erp_barcodes
set barcode_list = (\',\'', #curr_barcode, ') where ',
#erp_code_field_name, ' = ', #curr_erp_code);
Anyway, you should change it as #Phylogenesis told you in its comment.
Thank you for all your answers. I finally achieved it as follows:
set #q1 = concat('update lt_main_barcodes_v2
set barcode_list = concat(\',', #curr_barcode, '\', barcode_list)
where ', #erp_code_field_name, ' = ', #curr_erp_code);
I corrected syntax errors (thanks James) and added another concat(). Now, MySQL understands barcode_list is a field. Selecting #q1 gives brings the following output:
update lt_erp_barcodes
set barcode_list = concat(',123123123', barcode_list)
where cod_navision = 12345

Every time I try to execute the code below, it displays the error "Incorrect syntax near 'B'."

I am working on a project and I need to declare the query below. But every time I try to execute it, it displays the error "Incorrect syntax near 'B'."
DECLARE #strLineCode VARCHAR (20)
SET #strLineCode = 'CDP'
DECLARE #strSQL VARCHAR(5000)
SET #strSQL = 'SELECT B.strCurrentLineCode,
strLineMachineNo, strSAPAssetNum, B.strResourceSubType
FROM tblMachMaster A
INNER JOIN vwResourceMaster B
ON A.strResourceNumber = B.strResourceNumber'
SET #strSQL = #strSQL + 'WHERE B.strCurrentLineCode = ''' + #strLineCode + ''' '
EXEC(#strSQL)
I tried executing it without declaring the query and it works just fine. Send help :(
SET #strLineCode = 'CDP';
PREPARE stmt FROM 'SELECT B.strCurrentLineCode,
strLineMachineNo, strSAPAssetNum, B.strResourceSubType
FROM tblMachMaster A
INNER JOIN vwResourceMaster B
ON A.strResourceNumber = B.strResourceNumber WHERE B.strCurrentLineCode = ?' ;
EXECUTE stmt
USING #strLineCode;
You can try above code.
Here I had used PREPARE and EXECUTE Statement.
Using which you can simply execute your dynamic query.
One more thing you missed ';' after every line of code. In MySQL statement is always end with ';' only.

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 #.