Error Code: 1064 in stored procedure update - mysql

I am using a prepared statement to execute an update query but I am getting the following error.
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 'NULL' at line 1
Here is the query my prepared statement produces.
Declare update_table_stmt varchar(1024);
set #update_data = concat('update results_temp_tbl as t1 inner join data_temp_tbl as t2 on t2.suite_raw = t1.suite_raw set t1.`',#get_dataday,'` = ',#get_set_columns,';');
update results_temp_tbl as t1 inner join data_temp_tbl as t2 on t2.suite_raw = t1.suite_raw set t1.`17356` = concat(t2.path,'/',t2.filename);
PREPARE update_table_stmt FROM #update_data;
EXECUTE update_table_stmt;
DEALLOCATE PREPARE update_table_stmt;
If I copy the above query and run it, I get no errors. So I am unsure of how to solve the issue
*****update****
Still getting the error. I have adjusted the code so it now prints out the following.
update results_temp_tbl as t1 inner join data_temp_tbl as t2 on t2.suite_raw = t1.suite_raw set t1.`17356` = t2.filepath_name;

I figured it out. I was in a loop and the id was null

Related

Liquibase: <changeSet> with multiple SQL statements in <sql> using parameters

I need to run a prepared statement in Liquibase:
<changeSet id="53" author="foo">
<sql dbms="!h2, mysql" splitStatements="true">
SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
SET #constraint_name = (
SELECT rc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
ON kcu.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG
AND kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
WHERE kcu.TABLE_NAME = #table_name
AND kcu.COLUMN_NAME = #column_name
);
SET #s = concat('ALTER TABLE ', #table_name, ' DROP FOREIGN KEY ', #constraint_name);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt
</sql>
</changeSet>
If I set splitStatements to true, then I think parameters don't persist across statements so understandably I get an error saying that #s is NULL:
Reason: liquibase.exception.DatabaseException: 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 'NULL' at line 1 [Failed SQL: PREPARE stmt FROM #s]
If I set splitStatements to false, and also delete all but the first two SETs to make sure it's not the nested SELECT causing the issue, I get this:
Reason: liquibase.exception.DatabaseException: 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 'SET #column_name = 'ACTOR_ID'' at line 2 [Failed SQL: SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';]
I assumed then the issue with with the endDelimiter defaulting to ;. So I tried FOO:
<sql dbms="!h2, mysql" splitStatements="false" endDelimiter="FOO">
SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
FOO
</sql>
To which I get...
Reason: liquibase.exception.DatabaseException: 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 'SET #column_name = 'ACTOR_ID';
FOO' at line 2 [Failed SQL: SET #table_name = 'AUDIT_EVENT';
SET #column_name = 'ACTOR_ID';
FOO]
I feel like I don't understand what's going on. I checked SQL does work in MySQL Workbench. Any insights?
Update
So, I thought splitStatements=false got me past the NULL #s error, and therefore hypothesized that parameters (or "user/session variables" as I now know to call them) must not persist when split. But that was all wrong. Those variables do persist across statements: for the duration of the session.
splitStatements=false simply masked the original error with a new one, which did indeed have to do with MySQL not allowing multiple queries at once. When I unblocked that using allowMultiQueries=true, I got a new error pointing to Line 1, which eventually got me to fix the original error, which was that my table name was wrong (uppercase instead of lowercase).
What I never realized was that, the table name being wrong also contributed to #s ending up NULL.
Old "Solution"
I didn't realize that MySQL doesn't allow multiple queries by default. I had to add that in the JDBC URL as a query parameter:
jdbc:mysql://%s:%d/%s?user=root&allowMultiQueries=true
As I understand, this is not a portable path. But since I'm using Liquibase to specify exactly what database to run these statements on, I think it's okay.

Trigger with update other table, from aggregation of another table

i'm try to make a trigger on MySql.
Cenario:
i've 3 tables:
com_proposals
com_proposals_items
com_proposals_subitems
IMPORTANT: the three tables has this columns (value_gross,value_disc_auto,value_disc_comercial)
And the source of this values is aways from the last one - com_proposals_subitems
If i updates qty in second table, i've to update the principal table (com_proposals)
And that needs the calculation(sum) of third table(..subitems).
Look:
DELIMITER $$
CREATE TRIGGER trigger_update_com_proposals_cart_items_updatevalues
AFTER UPDATE ON com_proposals_cart_items
FOR EACH ROW
BEGIN
SET #id = OLD.id;
SET #prop_id = OLD.proposal_id;
SELECT
#gross := sum(value_gross),
#auto := sum(value_disc_auto),
#com := sum(value_disc_comercial)
from
com_proposals_cart_subitems
where
cart_item_id = #id;
OLD.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial = #com;
SELECT
#item_gross := sum(value_gross),
#item_auto := sum(value_disc_auto),
#item_com := sum(value_disc_comercial)
from
com_proposals_cart_items
where
id = #id;
UPDATE com_proposals set
value_gross = #item_gross,
value_disc_auto = #item_autoy,
value_disc_comercial = #item_com
WHERE id = #prop_id;
END$$
DELIMITER ;
But, i'm getting a syntax error:
Error occurred during SQL script execution
Razão:
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 '.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial' at line 17
Someone kwons whats happining?? THANKS A LOT!!!
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

MySQL Error 1064 : near NULL at line 1

I have been trying to figure out what am i doing wrong in the beloe procedure
CREATE PROCEDURE `example`(IN col_n char(50),IN p_cont char(50),IN p_ud int)
BEGIN
set #S = CONCAT('select rn.emp_id as emp_id,controller,perma,permb,permc,permd,order from emp n
join resource_emp rn on rn.emp_id = n.emp_id
join resource r on r.resource_id = rn.resource_id
join u_resource ur on ur.resource_id = r.resource_id
join user u on u.u_id = ur.ur_id
join(
select title,min(order) as pr from emp n
join resource_emp rn on rn.emp_id = n.emp_id
join resource r on r.resource_id = rn.resource_id
join u_resource ur on ur.resource_id = r.resource_id
join user u on u.u_id = ur.ur_id
where u.u_id = ',#p_ud,' group by title)a on a.title = n.title and a.pr = order
where u.u_id = ',#p_ud,' n.con = ''',#p_cont,''' and ,#col_n,' = 1
group by n.title order by n.order');
PREPARE stmt1 FROM #S;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END
I'm trying to execute the procedure as below but having the error
call hop_thlc_t.auth('perma', 'submit', 2);
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 'NULL' at line 1
while this happens, i can still execute the same and get desired results by running the above procedure as a query by using below params
set #col_n ='perma';
set #p_cont = 'submit' ;
set #p_ud =2;
Any help to fix the issue is appreciated
Just take the # off of your variables that are procedure arguments, as Sami Kuhmonen suggests in a comment.
Local variables in stored procedures don't have # before the name. If you use a variable like #col_n it's not the same variable as col_n which is the variable for your procedure argument.
Variables with # are called Session Variables. They have a scope outside your stored procedure. You discovered you can set the value before calling your procedure. Likewise, if you set the value of this variable inside the procedure, it will still have that value after your procedure returns.
I posted a demo in my answer here: https://stackoverflow.com/a/41925146/20860
Unfortunately, there's an exception to every rule. When you use PREPARE, you must use a session variable. Local variables won't work for PREPARE.

Error 1064:You have an error in your SQL syntax

I want run this query but get an error:
Error 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 'UPDATE `ads` SET `aDesc` = replace(aDesc, 'amp;', '')' at line 3
My query is:
UPDATE `ads`
SET `aName` = replace(aName, 'amp;', '')
UPDATE `ads`
SET `aDesc` = replace(aDesc, 'amp;', '');
What's the problem?
Your query looks like two queries without a separating delimiter.
The more efficient option is to do both changes in one query:
UPDATE ads
SET aName = replace(aName, 'amp;', ''),
aDesc = replace(aDesc, 'amp;', '');
but if you must run two queries:
UPDATE ads SET aName = replace(aName, 'amp;', '');
UPDATE ads SET aDesc = replace(aDesc, 'amp;', '');

Unidentifiable error in SQL syntax, thrown when trying to store a procedure

I am getting an error when I try to enter the following procedure using the phpMyAdmin SQL box. I'm pretty sure the syntax is correct though!
ERROR:
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
')
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = de' at line 1
SQL SYNTAX:
delimiter $$
CREATE PROCEDURE DeleteCard(IN aCard varChar)
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = decks.DeckName
SET decks.DeckTotal = decks.DeckTotal - amount.Amount
WHERE amount.CardName = aCard ;
UPDATE types t1
JOIN cards ON cards.TypeName = t1.TypeName
JOIN Amount ON amount.CardName = cards.CardName
SET t1.TypeTotal = t1.TypeTotal - amount.Amount
WHERE amount.CardName = aCard ;
DELETE
FROM amount
WHERE cardName = aCard;
DELETE
FROM cards
WHERE cardName = aCard;
END
$$
DELIMITER ;