【问题标题】:Spring boot JPA nativeQuery Incorrect syntax near @P0Spring boot JPA nativeQuery @P0 附近的语法不正确
【发布时间】:2020-10-01 16:54:18
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: java sql spring hibernate jpa


    【解决方案1】:

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

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

    【讨论】:

    • 这将返回 com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '('.
    • 尝试改变方言
    • 实际上,这就是问题所在。原来Hibernate不支持所有方言
    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 2011-10-25
    • 2019-12-30
    • 2015-12-12
    • 2022-01-24
    • 2013-12-16
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多