Syntax error at or near at character

PostgreSQL error 42601 mainly occurs due to the syntax errors in the code. Proper syntax check and code correction will fix it up.

Syntax errors are quite common while coding.

But, things go for a toss when it results in website errors.

PostgreSQL error 42601 also occurs due to syntax errors in the database queries.

At Bobcares, we often get requests from PostgreSQL users to fix errors as part of our Server Management Services.

Today, let’s check PostgreSQL error in detail and see how our Support Engineers fix it for the customers.

What causes error 42601 in PostgreSQL?

PostgreSQL is an advanced database engine. It is popular for its extensive features and ability to handle complex database situations.

Applications like Instagram, Facebook, Apple, etc rely on the PostgreSQL database.

But what causes error 42601?

PostgreSQL error codes consist of five characters. The first two characters denote the class of errors. And the remaining three characters indicate a specific condition within that class.

Here, 42 in 42601 represent the class “Syntax Error or Access Rule Violation“.

In short, this error mainly occurs due to the syntax errors in the queries executed. A typical error shows up as:

Here, the syntax error has occurred in position 119 near the value “parents” in the query.

How we fix the error?

Now let’s see how our PostgreSQL engineers resolve this error efficiently.

Recently, one of our customers contacted us with this error. He tried to execute the following code,

CREATE OR REPLACE FUNCTION prc_tst_bulk(sql text)
RETURNS TABLE (name text, rowcount integer) AS
$$
BEGIN
WITH m_ty_person AS (return query execute sql)
select name, count(*) from m_ty_person where name like '%a%' group by name
union
select name, count(*) from m_ty_person where gender = 1 group by name;
END
$$ LANGUAGE plpgsql;

But, this ended up in PostgreSQL error 42601. And he got the following error message,

ERROR: syntax error at or near "return"
LINE 5: WITH m_ty_person AS (return query execute sql)

Our PostgreSQL Engineers checked the issue and found out the syntax error. The statement in Line 5 was a mix of plain and dynamic SQL. In general, the PostgreSQL query should be either fully dynamic or plain. Therefore, we changed the code as,

RETURN QUERY EXECUTE '
WITH m_ty_person AS (' || sql || $x$)
SELECT name, count(*)::int FROM m_ty_person WHERE name LIKE '%a%' GROUP BY name
UNION
SELECT name, count(*)::int FROM m_ty_person WHERE gender = 1 GROUP BY name$x$;

This resolved the error 42601, and the code worked fine.

[Need more assistance to solve PostgreSQL error 42601?- We’ll help you.]

Conclusion

In short, PostgreSQL error 42601 occurs due to the syntax errors in the code. Today, in this write-up, we have discussed how our Support Engineers fixed this error for our customers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

I am creating a java web application using MyBatis and postgres.
If i execute insert query as shown below, an error will occur.

■ MyBatis xml file

<insert id = "insertRace">
        insert into t_race (race_id, obstacle_flg, race_name, grade, race_number, field_type, distance, rotate_type, weather, field_condition, host_id, post_time, host_date, host_times, host_days, horse_type, age_condition, product_condition, appointed_condition, sex_condition, weight_condition, insert_date, insert_user, update_date, update_user)
        values ​​('${raceId}', ${obstacleFlg}, '${raceName}', '${grade}', ${raceNumber}, '${fieldType}', ${distance}, '${rotateType} ',' ${weather} ',' ${fieldCondition} ',' ${hostId} ', ${postTime}, ${hostDate}, ${hostTimes}, ${hostDays}, ${horseType},' ${ageCondition} ', ${productCondition}, ${appointedCondition}, ${sexCondition}, ${weightCondition}, CURRENT_TIMESTAMP,' SYSTEM ', CURRENT_TIMESTAMP,' SYSTEM ')
    </insert>

■ Errors output by postgres

ERROR: syntax error at or near ":" at character 433
STATEMENT: insert into t_race (race_id, obstacle_flg, race_name, grade, race_number, field_type, distance, rotate_type, weather, field_condition, host_id, post_time, host_date, host_times, host_days, horse_type, age_condition, product_condition, appointed_condition, sex_condition, weight_condition, insert_date, insert_user, update_date, update_user)
            values ​​('201606020302', 0, '3 years old not won', 'not won', 2, 'dirt', 1200, 'right', 'cloudy', 'good', '06', 10:40:00, 2016-03-05, 2, 3, 1, '3 years old', 1, 2, 999, 4, CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM')

■ java console exception

### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
  Position: 433
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into t_race (race_id, obstacle_flg, race_name, grade, race_number, field_type, distance, rotate_type, weather, field_condition, host_id, post_time, host_date, host_times, host_days, horse_type, age_condition, product_condition, appointed_condition, sex_condition, weight_condition , insert_date, insert_user, update_date, update_user) values ​​('201606020302', 0, '3 years old not won', 'not won', 2, 'dirt', 1200, 'right', 'cloudy', 'good', ' 06 ', 10:40:00, 2016-03-05, 2, 3, 1,' 3 years old ', 1, 2, 999, 4, CURRENT_TIMESTAMP,' SYSTEM ', CURRENT_TIMESTAMP,' SYSTEM ')
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
  Position: 433
bad SQL grammar [];nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
  Position: 433] with root cause
org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
  Position: 433
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute (AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags (AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute (AbstractJdbc2Statement.java:381)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update (PreparedStatementHandler.java:45)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update (RoutingStatementHandler.java:73)at org.apache.ibatis.executor.SimpleExecutor.doUpdate (SimpleExecutor.java:49)
    at org.apache.ibatis.executor.BaseExecutor.update (BaseExecutor.java:115)
    at org.apache.ibatis.executor.CachingExecutor.update (CachingExecutor.java:75)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update (DefaultSqlSession.java:170)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert (DefaultSqlSession.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:483)
    at org.mybatis.spring.SqlSessionTemplate $SqlSessionInterceptor.invoke (SqlSessionTemplate.java:358)
    at com.sun.proxy. $Proxy12.insert (Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert (SqlSessionTemplate.java:240)
    at org.apache.ibatis.binding.MapperMethod.execute (MapperMethod.java:52)
    at org.apache.ibatis.binding.MapperProxy.invoke (MapperProxy.java:53)
    at com.sun.proxy. $Proxy24.insertRace (Unknown Source)
    at jp.keiba.analitics.service.ScrapingDataService.insertRace (ScrapingDataService.java:169)
    at jp.keiba.analitics.service.ScrapingDataService.insertAll (ScrapingDataService.java:45)
    at jp.keiba.analitics.controller.scraping.UrlRaceScrpController.raceComplete (UrlRaceScrpController.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAdapter.java:817)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter.java:731)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:968)at org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.java:859)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:844)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:208)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (CharacterEncodingFilter.java:121)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process (AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol $AbstractConnectionHandler.process (AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint $SocketProcessor.run (JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor $Worker.run (ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread $WrappingRunnable.run (TaskThread.java:61)
    at java.lang.Thread.run (Thread.java:745)

I don’t use»:»in the query and I’m not sure why the error message is ERROR: syntax error at or near»:».
We are very happy if you can help us.

Introduction

Actually, this article has a relation with the existence of the previous article. That previous article exist in this link with the title of ‘How to Solve Error Message Model Attribute Problem SyntaxError: invalid syntax in Django Application’. It is actually just inappropriate format of the column name available in the SQL file. That SQL file actually containing an INSERT statement for restoring data to the targeted database. But since there is a column name which is not following the standard rule which starts with a character that is not number or letter, it cause the restore process to fail.

The following is just to describe that accessing the database is not the cause of the problem.

Microsoft Windows [Version 10.0.19042.1288]
(c) Microsoft Corporation. All rights reserved.

C:UsersPersonal>cd 

C:>psql -Upostgres -d db_app
Password for user postgres:
psql (14.0)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.

db_app=# q

After that, the process for inserting records by importing it or restoring it using the following command exist as follows :

C:>psql -Uuser_app -d db_app < "C:UsersPersonalDownloadsinsert-current-product.sql"
Password for user db_user:
ERROR: syntax error at or near "["
LINE 1: ...,[product_code...
^

As in the above command execution, it fail with an error message appear.

Solution

Actually, the solution for the above error message causing it is because of the column’s character is not a proper name for a column name. In that case, just change it into a proper one. So, edit the SQL file and find the column’s character or the column name which is the cause for the database restore process to fail. Changing the column name from [product_code] to another proper one. That new column name is ‘product_code’. After editing the file, just execute the process for importing or restoring the data once more as follows :

C:>psql -Uuser_sinergi -d db_sinergi < "C:UsersPersonalDownloadsinsert-current-product.sql"
Password for user db_user:
INSERT 0 556

C:>

Fortunately, the process is a success as in the output of the command above.

Понравилась статья? Поделить с друзьями:
  • Svchost exe системное предупреждение unknown hard error
  • Syntax error at end of input перевод
  • Syntax error at end of input sqlstate 42601
  • Syntax error at end of input postgresql
  • Svchost exe системная ошибка как исправить