Spring Boot JPA 原生查询语句中 @P0 附近语法错误。

huangapple go评论63阅读模式
英文:

Spring boot JPA nativeQuery Incorrect syntax near @P0

问题

@Query(
   value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK ?, SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
   nativeQuery = true)
Integer insertImage(String path);

错误信息:

com.microsoft.sqlserver.jdbc.SQLServerException: 在“@P0”附近有语法错误。

如果我手动编写路径,它会正常工作:

@Query(
   value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK 'C:\\pic\\NORMAL2-IM-1257-0001.jpeg', SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
   nativeQuery = true)
Integer insertImage(String path);
英文:

I am trying to run the following query from a spring boot app using Hibernate, and the problem is that in the query I need to pass the path variable from the method.

@Query(
   value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK ?, SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
   nativeQuery = true)
Integer insertImage(String path);

the error I am getting is

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.

If I write the path manually it will work

@Query(
   value = "INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK 'C:\\pic\\NORMAL2-IM-1257-0001.jpeg', SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);",
   nativeQuery = true)
Integer insertImage(String path);

答案1

得分: 1

'@P0'是您的RowCountToDisplay参数... 可能尝试在参数周围加上括号

INSERT INTO dbo.images (imageblob) (SELECT * FROM OPENROWSET (BULK (?), SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);
", nativeQuery = true)
英文:

'@P0' is your RowCountToDisplay parameter ... Maybe try to place parenthesis around the argument

INSERT INTO dbo.images (imageblob)(SELECT * FROM OPENROWSET (BULK (?), SINGLE_BLOB) imageblol) SELECT CAST(scope_identity() AS int);"
             , nativeQuery = true)

huangapple
  • 本文由 发表于 2020年10月2日 00:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64159974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定