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.
Related
I want to add a not null column to a table with existing data. My toolset includes MariaDb and flyway. Here's what I am doing
IF NOT EXISTS(SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MY_DATA_TABLE'
AND table_schema = '${schemaName}'
AND column_name = 'NewColumnName'
) THEN
ALTER TABLE MY_DATA_TABLE ADD COLUMN 'NewColumnName' INT;
SELECT ID INTO #val FROM MASTER_TABLE WHERE lower(Name) = 'XYZ';
UPDATE MY_DATA_TABLE SET NewColumnName = #val;
ALTER TABLE MY_DATA_TABLE MODIFY COLUMN 'NewColumnName' INT NOT NULL;
END IF;
Doing mvn flyway:migrate gives me this error
[ERROR] SQL State : 42000
[ERROR] Error Code : 1064
[ERROR] Message : 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 ''NewColumnName'
INT' at line 7
I even tried placing some running select statement inside, but the error remains the same. Please suggest some workaround. Please also recommend if there's another way to achieve the objective.
Thanks!
I suspect the issue is with quoting the column name. Try executing the SQL interactively and see if you get the same error. Then try and get it working successfully. In a related question the answer was to try no quotes around column names or to escape them with back ticks.
Here's how I ended up to meet the desired.
DROP PROCEDURE IF EXISTS `proc_UpdateMyColumn`;
DELIMITER //
CREATE PROCEDURE `proc_UpdateMyColumn`
(
)
BEGIN
DECLARE valueToSet INT;
IF NOT EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MY_DATA_TABLE'
AND table_schema = '${schemaName}'
AND column_name = 'NewColumnName'
) THEN
ALTER TABLE MY_DATA_TABLE ADD COLUMN NewColumnName INT;
SELECT ID INTO valueToSet FROM MASTER_TABLE WHERE lower(Name) = 'XYZ';
UPDATE MY_DATA_TABLE SET NewColumnName = valueToSet;
ALTER TABLE MY_DATA_TABLE MODIFY COLUMN NewColumnName INT NOT NULL;
END IF;
END;
DELIMITER;
CALL `proc_UpdateMyColumn`();
DELIMITER;
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
I'm converting all of my existing MSSQL databases and stored procedures am stuck on a new stored procedure where I need to update an existing record. The procedure gets called from a web form once a record has been inserted into the database and en email sent successfully (or at least passed off to the SMTP server)
I've had a working procedure in MSSQL for a long time but am trying to convert it to MySQL. I'm passing in 3 variables - a bit indicating the email got sent, a string indicating which SMTP server has been used to sent the email and a unique record id so I'll know what record to update. I'm also adding the date and time to another field to know when the procedure ran.
I've got the following but keep getting an 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 '' at line 7 - yet I don't see anything off at line 7 - at least to my eye.
The code I'm trying to use is:
CREATE PROCEDURE `sp_Test`(
`emailSent_In` BIGINT UNSIGNED,
`emailTransport_In` VARCHAR(100),
`formSecret_In` VARCHAR(32)
)
BEGIN
SET #`query` := CONCAT('UPDATE ',`tbl_JustSayThanks`,'
SET `emailSent` = `emailSent_In`,
`emailTransport` = ',`emailTransport_In`,',
`emailSentDate` = NOW()
WHERE `formSecret` = ', `formSecret_In`, '');
PREPARE `stmt` FROM #`query`;
EXECUTE `stmt`;
#`query` := NULL;
DEALLOCATE PREPARE `stmt`;
END//
DELIMITER ;
Just FYI, I'm using the CONCAT based on a previous answer I received from wchiquito and will be passing in the table name eventually. But, I wanted to get it to work on a simplified level before going there.
The following is wrong:
SET #`query` := CONCAT('UPDATE ',`tbl_JustSayThanks`,'
because you seem to be concatenating your SQL text with the value of tbl_JustSayThanks, but I think you mean to use the identifier itself. This should therefore be:
SET #`query` := CONCAT('UPDATE `tbl_JustSayThanks`',
The following is wrong:
`emailTransport` = ',`emailTransport_In`,',
because the variable is a VARCHAR but you don't quote it as a string literal in your SQL statement. It's easy to get mixed up with the multiple levels of quoting. It should be:
`emailTransport` = ''', `emailTransport_In`, ''',
The following is wrong for the same reason:
WHERE `formSecret` = ', `formSecret_In`, '');
it should be:
WHERE `formSecret` = ''', `formSecret_In`, '''');
This still suffers from SQL injection problems, unless you can guarantee that the input parameters are safe (which is not a good assumption). If you need to concatenate values into your SQL expressions, you should use the QUOTE() function to do escaping:
SET #query = CONCAT('
UPDATE tbl_JustSayThanks
SET emailSent = ', QUOTE(emailSent_In), '
emailTransport = ', QUOTE(emailTransport_In), '
emailSentDate = NOW()
WHERE formSecret = ', QUOTE(formSecret_In));
More comments:
You don't need to delimit every identifier with back-ticks, only those that conflict with SQL reserved words, or contain whitespace or punctuation or international characters. None of your identifiers you show require delimiting.
When you use prepared statements, you should use query parameters with the ? placeholders, intead of concatenating variables into the SQL string. You don't quote parameter placeholders in your SQL query. That way you won't run into hard-to-debug syntax errors like the ones you found.
Here's an example showing the fixes:
CREATE PROCEDURE sp_Test(
emailSent_In BIGINT UNSIGNED,
emailTransport_In VARCHAR(100),
formSecret_In VARCHAR(32)
)
BEGIN
SET #query = '
UPDATE tbl_JustSayThanks
SET emailSent = ?,
emailTransport = ?,
emailSentDate = NOW()
WHERE formSecret = ?';
SET #es = emailSent_In;
SET #et = emailTransport_In;
SET #fs = formSecret_In;
PREPARE stmt FROM #query;
EXECUTE stmt USING #es, #et, #fs;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
Final comment:
Your example query has no dynamic syntax elements, only dynamic values. So you don't need to use a prepared statement at all.
This is how I'd really write the procedure:
CREATE PROCEDURE sp_Test(
emailSent_In BIGINT UNSIGNED,
emailTransport_In VARCHAR(100),
formSecret_In VARCHAR(32)
)
BEGIN
UPDATE tbl_JustSayThanks
SET emailSent = emailSent_In,
emailTransport = emailTransport_In,
emailSentDate = NOW()
WHERE formSecret = formSecret_In;
END//
DELIMITER ;
You should also be aware that MySQL stored procedures are greatly inferior to Microsoft SQL Server. MySQL doesn't keep compiled stored procedures, it doesn't support packages, it doesn't have a debugger... I recommend you do not use MySQL stored procedures. Use application code instead.
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;', '');
I have some stored procedures and a trigger that work great in MySQL 5.5.8 but for some reason don't work in 5.1. The error descriptions aren't enough for me to figure out the problem. Here is the code and the errors.
CREATE PROCEDURE `cg_getMatchingContent`(
MatchTerm VARCHAR(255),
MaxResults INT)
BEGIN
SELECT * FROM (
SELECT t.*, INSTR(t.`Title`,MatchTerm) as Pos
FROM cg_content t ) c
WHERE Pos>0 ORDER BY Pos LIMIT 0, MaxResults;
END
Error: 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 'MaxResults' at line 8
DELIMITER ;;
CREATE TRIGGER `cg`.`cg_content_UrlDup_ConstTrig`
BEFORE INSERT ON `cg`.`cg_content`
FOR EACH ROW
Begin
DECLARE errorString VARCHAR(500);
DECLARE insert_error CONDITION FOR SQLSTATE '99001';
IF new.Url = '' THEN
SET errorString = CONCAT('Url cannot be blank
Title: ' , new.Title);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
END if;
IF Exists(SELECT id FROM cg.cg_content WHERE Url=new.Url) THEN
SET errorString = CONCAT('Url is not unique
Title: ' , new.Title , '
Url: ' + new.Url);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
End if;
End ;;
Error: 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 'insert_error
SET MESSAGE_TEXT=errorString;END if;IF ' at line 10
From the docs:
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
5.1 does not support variables in LIMIT and OFFSET.
The second one is easy to figure, hard to fix. SIGNAL and RESIGNAL commands were introduced in MySQL 5.5. You can't convert it easily to 5.1. One way to do it, would be to run a query that errors. For example a SELECT from a non-existent table.