首页 > 解决方案 > Spring boot JPA nativeQuery @P0 附近的语法不正确

问题描述

我正在尝试使用 Hibernate 从 Spring Boot 应用程序运行以下查询,问题是在查询中我需要从方法中传递路径变量。

@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: Incorrect syntax near '@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);

标签: javasqlspringhibernatejpa

解决方案


'@P0' 是您的 RowCountToDisplay 参数...也许尝试在参数周围放置括号

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

推荐阅读