I'm trying to run the query below, that is stored in a .sql file, then read with ioutils.ReadFile and executed on initialization
CREATE TABLE IF NOT EXISTS districts
(
GeoCode integer PRIMARY KEY,
name varchar(32)
);
drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert_district(in pgeocode int, in pname varchar(32))
BEGIN
INSERT INTO districts(geocode, name) VALUES (pgeocode, pname);
SELECT * FROM districts where geocode = pgeocode;
END$$
DELIMITER ;
I am using the database/sql package and run the query with Exec
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 'drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert' at line 7
To the best of my knowledge my syntax is correct, and I tested it so I cannot figure out why the same exact query cannot be run properly from the program.
The Go MySQL client does not support multiple SQL statements by default. You can't just feed it a text file with ; separated statements.
See Does a Go Mysql driver exist that supports multiple statements within a single string? for details — there's an option you can use to allow multi-statements.
But that still won't support statements like DELIMITER which are not recognized by the MySQL Server. That's a mysql client command.
You have two alternatives:
Parse the .sql file to find statement terminators and run the statements one at a time.
Execute the mysql client in a sub-process using the .sql file as input.
Related
I want to run sql script which include db and table creations and stored procedure creations.
but when I try to run sql script using execute sql script keyword in database library I get an error like below
ProgrammingError: (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 'DELIMITER $$\n CREATE OR
REPLACE PROCEDURE `proc_GetCustomerDetails`(\n I...' at line 2")
before procedure I have delimiter like this,
DELIMITER $$
CREATE OR REPLACE PROCEDURE `proc_GetCustomerDetails`(
IN CustomerNbr LONGTEXT,
IN Lang VARCHAR(5)
)
DETERMINISTIC
BEGIN
IF Lang IS NULL THEN SET lang = "fin";
END IF;
SELECT * from dbname.customer;
END;$$
DELIMITER ;
If I comment stored procedure part, sql file is running without errors with rest of the table creation statements.
I googled this and couldn't find any related issue. I saw we have call stored procedure keyword. but I want to keep table creations and stored procedures in same sql file and need to run. I use MariaDB for this task.
Libraries used,
pymysql
robotframework-databaselibrary
If I run sql file using HeidiSQL it is running without any errors with procedures and delimiters. That mean there are no sql errors.
Can Someone tell me how to fix this?
DELIMITER is a statement supported only for the client, it is not supported by the server; thus the error. The solution - drop it.
Here's a question with very good answers what is it and why it's needed.
In short - when you work with a client you need a way to instruct it "this is not a statement you should execute immediately, this is still just a line in the SP you'll be sending to the server" - so you tell (to the client) "the DELIMITER b/n statements is temporarily $$". The server doesn't need/care about that - it knows everything between CREATE PROCEDURE, BEGIN, END are connected statements, a block.
When you connect to the DB through API (pymysql) vs an interactive client (shell, heidisql, etc) - you're sending the SP as a block, there's no way its statements will be ran one by one, thus the DELIMITER is not needed, not a supported command by the server, and generates an error. Drop it.
I'm using the following SQL Script in my IDE DBeaver, MySQL 8.0.21 Linux (Docker Container). Database is in utf8mb4 / utf8mb4_general_ci encodings.
DELIMITER //
CREATE OR REPLACE TRIGGER trg_line_total
BEFORE INSERT ON LINE
FOR EACH ROW
BEGIN
SET NEW.LINE_TOTAL = NEW.LINE_UNITS * NEW.LINE_PRICE;
END //
DELIMITER ;
It seems to be valid SQL, but it is returning the following error, as if it wasn't reading the full line. I can remove tabs, line returns and it will read more or less characters.
Error occurred during SQL script execution
Reason:
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 'TRIGGER trg_line_total
BEFORE INSERT ON LINE
FOR EACH ROW
BEGIN
SET NEW.LINE_TO' at line 1
I already seemingly fixed this error once today by deleting everything I had in my editor and typing it out from scratch, as if there was some invisible line ending that was messing things up or getting interpreted. I looked in vim and used the :set list command and I see are regular line return carriages. What could it be?
Unfortunately, there's no ALTER TRIGGER or CREATE OR REPLACE TRIGGER in Oracle's MySQL. It supports only Create Trigger format.
For MariaDB, in version 10.1.4, added support CREATE OR REPLACE TRIGGER to their drop-in replacement for MySQL.
If you are sure that trigger doesn't exists
Use CREATE instead of CREATE OR REPLACE.
If you are modifying existing trigger
I would suggest that the best practice is to lock the table where the trigger lives, so no rows are impacted with the trigger absent. Dropping and adding triggers while a table is locked is allowed.
mysql> LOCK TABLES t1 WRITE; -- the next prompt appears once you've obtained the lock
mysql> DROP TRIGGER t1_bi;
mysql> DELIMITER $$
mysql> CREATE TRIGGER ti_bi BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
...
END $$
mysql> DELIMITER ;
mysql> UNLOCK TABLES;
Reference: Modify Existing Trigger Definition in MySQL
I have a stored procedure designed to generate a new, 'derived' table. In this procedure I then want to add a column using ALTER TABLE. However, despite an almost identical stored procedure working fine, and despite being able to add this manually as a stored procedure to the database using MySQL Workbench, when I pass the code to the server using SOURCE (i.e. SOURCE workload.sql), I get an error 1146 (42502) 'Table 'workload._convenor_workload' doesn't exist.' (I'm doing this in Emacs as part of a org-babel block, but this is essentially just passing raw SQL to the server.)
As background, I'm in the process of migrating SQL code from a setting where I was running it raw to create my final database to one where I'd like this code to be called via triggers.
Setup: mysql Ver 8.0.16 for macos10.14 on x86_64 (MySQL Community Server - GPL)
I've tried rewriting this as a prepared statement, was unsuccessful, and have been scouring Stack Overflow. This is my first MySQL project and my reading of the documentation suggests that ALTER TABLE is a perfectly legal thing to do in a stored procedure. It's likely that I'm making a schoolboy error somewhere but at the moment I'm banging my head.
Elsewhere in my SQL, this code works in a stored procedure (ALTER TABLE function does not throw an error):
CREATE TABLE _assessment_allocations AS SELECT Assessment_ID,
IFNULL(SUM(_total_first_marking_hours),0) AS _total_first_marking_hours_sum,
GROUP_CONCAT(DISTINCT _total_first_marking_hours_needed) AS _total_first_marking_hours_needed,
GROUP_CONCAT(DISTINCT Prog_ID) AS prog_id
FROM
_marking_workload
GROUP BY Prog_ID, Assessment_ID;
ALTER TABLE _assessment_allocations
ADD COLUMN _assessment_variance DECIMAL(5,2);
However, the code that throws the error is this (specifically, the ALTER TABLE function; I've added the stored procedure code in case this is helpful). Note that this code does not throw an error when ingested by MySQL outside a stored procedure:
USE `workload`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `module_administration_convenor`()
-- Begin by selecting elements of the ~modules~ table
CREATE TABLE `_convenor_workload` AS
SELECT Modules.Module_Code,
Modules.Module_Name,
Modules.Module_Convenor_ID,
Modules.Module_Convenor_Share,
Modules.Student_Tally,
Modules.Additional_Hours,
Modules.Convening_Notes,
Modules.Active_Status
FROM modules;
-- Add a 'Convenor' column
ALTER TABLE `_convenor_workload` ADD COLUMN `Name` VARCHAR(255) DEFAULT 'Convenor';
\* Other stuff *\
END$$
DELIMITER ;
My aim is to avoid throwing this error. I'd like to get this stored procedure actually stored! (Just like the previous stored procedure that does much the same and does not throw an error.) I'm aware that there are some back-tick and style differences between the working and non-working code, but I'm guessing these aren't super important.
As I said, I have a strong suspicion that I'm overlooking something obvious here...
As mentioned by Solarflare in the comments, you are missing a begin so the alter table is executing as a separate action. If you wrap it with begin and end then it treats all the code as the stored procedure.
USE `workload`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `module_administration_convenor`()
Begin
-- Begin by selecting elements of the ~modules~ table
CREATE TABLE `_convenor_workload` AS
SELECT Modules.Module_Code,
Modules.Module_Name,
Modules.Module_Convenor_ID,
Modules.Module_Convenor_Share,
Modules.Student_Tally,
Modules.Additional_Hours,
Modules.Convening_Notes,
Modules.Active_Status
FROM modules;
-- Add a 'Convenor' column
ALTER TABLE `_convenor_workload` ADD COLUMN `Name` VARCHAR(255) DEFAULT 'Convenor';
END
$$
DELIMITER ;
I'm working in mysql workbench 8.0.11 and having problems creating stored procedures and also functions. Just to be clear, I am running this in mysql via the mysql workbench. I am not using any other program at all.
I'm trying to create a new stored procedure. I keep getting a syntax error: "delimiter" is no valid input at this position, expecting: CREATE.
I have checked the mysql website to make sure my syntax is correct, and it matches. Another thing to note is that I've tried starting the stored procedure with use accounting; (the db I'm working with) and I get the same error, except that "use" takes the place of "delimiter". So, I'm not sure it has anything to do with the delimiter keyword itself. Is there some setting in mysql workbench that I can set to get this straightened out? Also, I get the same exact syntax error when trying to create a function.
I have tried creating both a stored procedure and a function without using the delimiter keyword, or the use keyword, and when I hit the apply key mysql crashes.
Here is my code:
delimiter $$
create procedure 'add_expense_category' (id int, name varchar(20))
begin
insert into expense_categories(expense_category_id, expense_category)
values(id, name);
end $$
delimiter ;
Anyone have any ideas on how to solve this? Settings to change? Anything?
Thanks!
try this
delimiter $$
create procedure `add_expense_category` (id int, name varchar(20))
begin
insert into expense_categories(expense_category_id, expense_category)
values(id, name);
end $$
delimiter ;
I have an application using MyBatis, and I'm trying to add a select SQL that has multiple statements.
<select id="getReleaseDetails" resultType="maingrid" statementType="STATEMENT">
DROP TEMPORARY TABLE IF EXISTS vrTmp;
DROP TEMPORARY TABLE IF EXISTS vrTmp2;
CREATE TEMPORARY TABLE vrTmp AS (
SELECT vr.*, v.Code
...etc.
However, I keep getting the error:
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'DROP TEMPORARY TABLE IF EXISTS vrTmp2;
CREATE TEMPORARY TABLE vrTmp AS (
S' at line 2
]
2014-03-11 12:48:20,069 [bio-8080-exec-3] DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
2014-03-11 12:48:20,085 [bio-8080-exec-3] ERROR VRMService - Error in getDetails :
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'DROP TEMPORARY TABLE IF EXISTS vrTmp2;
CREATE TEMPORARY TABLE vrTmp AS (
S' at line 2
The error may exist in mappings/mapping.xml
The SQL works fine in SQLYog, using the same database login.
What am I missing? It's almost like it does not like the ";" to separate the individual statements.
Thanks
Figured it out: by default the MySQL connection does not allow execution of multiple SQLs. It has to be specified in the URL:
url="jdbc:mysql://ln-ct-dv-my1:3306/TestDB?allowMultiQueries=true"
... and I had to remove the "StatementType" from the mapping file.
Here are some problems.
You can not use multiple statements in single tag in mybatis. Hence you can not use ";"
If at all if you want to use create table statement use insert tag.
The solution would be to use stored procedure to run multiple statements.
Here is an example for stored procedure and how to call it from mybatis.
CREATE OR REPLACE PROCEDURE KP_PRC AS
BEGIN
execute immediate 'DROP TEMPORARY TABLE IF EXISTS vrTmp';
execute immediate 'DROP TEMPORARY TABLE IF EXISTS vrTmp2';
execute immediate 'CREATE TEMPORARY TABLE vrTmp AS (SELECT vr.*, v.Code';
commit;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END KP_PRC;
Mybatis xml.
<select id="kpSpCall" statementType="CALLABLE">
{CALL KP_PRC()}
</select>