This is my Code of Stored Function:
CREATE DEFINER=`root`#`localhost` FUNCTION `func`()
RETURNS int(11)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE total INT DEFAULT 0;
select count(*) INTO total from students;
RETURN total;
END
Java Code:
query=em.createNativeQuery("{ SELECT func() }");
String s=(String) query.getSingleResult();
System.out.println(s);
But ir gives an Exception:
Internal Exception: 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 '{ SELECT func() }' at line 1
Error Code: 1064
Call: { SELECT func() }
Query: DataReadQuery(sql="{ SELECT func() }")
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 '{ SELECT func() }' at line 1
Error Code: 1064
Call: { SELECT func() }
Query: DataReadQuery(sql="{ SELECT func() }")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:377)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:516)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400)
at org.ceynamon.oregano.datamigration.Test.main(Test.java:26)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
The braces around SELECT funct() are not needed
Related
I'm using Node.js and TypeORM with MySQL but the error shows MariaDB I don't know why. I'm trying to use array in User entity and this must be the error
ERROR:
QueryFailedError: ER_PARSE_ERROR: 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 1
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "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 1",
sqlState: '42000',
index: 0,
sql: 'ALTER TABLE `user` ADD `favorites` text NOT NULL DEFAULT '
},
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "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 1",
sqlState: '42000',
index: 0,
sql: 'ALTER TABLE `user` ADD `favorites` text NOT NULL DEFAULT '
}
The columns causing the error:
#Column({ type: "simple-array", default: [] })
favorites: string[];
#Column({ type: "simple-array", default: [] })
cart: string[];
You can't have default value for column type text in mysql. Remove the default parameter in the column options and the query should work.
If you still want to have any default value set, you can write a native mysql trigger or use typeorm #AfterInsert hook to set the value of columns cart or favorites just after they're inserted in the database.
I have this mapper:
<insert id="insertBatch" parameterType="java.util.Set">
<foreach collection="filterParameterEntitySet" item="item" separator=";">
INSERT INTO filter_parameter
(
filter_key,
filter_value,
filter_id
)
VALUES
(
#{item.filterKey},
#{item.filterValue},
#{item.filter.id}
)
ON CONFLICT DO NOTHING
</foreach>
</insert>
Whenever I execute it, it throws BadSQLGrammarException:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: 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 'CONFLICT DO NOTHING
**
;
INSERT INTO filter_parameter
f' at line 13**
What could be the problem? I can't figure out where does the syntax error lie!
Try this:
<insert id="insertBatch" parameterType="java.util.Set">
INSERT INTO filter_parameter
(
filter_key,
filter_value,
filter_id
) VALUES
<foreach collection="filterParameterEntitySet" item="item" separator=",">
(
#{item.filterKey},
#{item.filterValue},
#{item.filter.id}
)
</foreach>
ON CONFLICT DO NOTHING
</insert>
So basically, there were 2 mistakes in the query:
The separator, instead of this: separator=";"
I should use this: separator=","
ON CONFLICT DO NOTHING is not valid in mysql and I was using mysql (it's for postgres), so I removed it and added the keyword IGNORE after INSERT to reserve the same functionality
Simple table:
CREATE TABLE `LocalTest` (
`mainJson` json NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Attempt to insert json value:
insert into `LocalTest` (mainJson) values ('{ "key": "value" }')
As result 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 'Error Code: 3140. Invalid JSON text: "The document root must not be followed by ' at line 3
What wrong with this simplest script?
I use Liquibase on Mysql and got a below error when trying create function
Unexpected error running Liquibase: 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 'in varchar2) return varchar2
is
This is my function file MyFunction.sql:
create or replace function MyFuncion(p_id in varchar2)
return varchar2 is Result varchar2(50);
begin Result := 'bbbb' || p_id;
return(Result); end MyFuncion;
End my changelog
<changeSet author="taibc" id="sqlFile-example2">
<sqlFile dbms="ttdp_thanh, mysql"
encoding="utf8"
endDelimiter="\n/"
path="MyFunction.sql"
relativeToChangelogFile="true"
splitStatements="true"
stripComments="true"/>
<rollback>
DROP Function MyFunction;
</rollback>
</changeSet>
Can you help me ?
SELECT `template_id`, replace(template_code,"AT -","DE -") as
template_code, `template_text`, `template_styles`, `template_type`,
`template_subject`, `template_sender_name`, `template_sender_email`,
`added_at`, `modified_at`, `orig_template_code`, `orig_template_variables`
FROM `core_email_template` WHERE template_code like "%AT - %"
This query it works and returns me some data that I want to export it. But when I do that from phpmyadmin I received this error:
Error reading data: (#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 'FROM WHERE
(`core_email_template`.`template_id` = 27) OR (`core_email_template' at
line 1)
What should I have to do to make this work ? thx