Error: Incorrect syntax near (database name)

Error: Incorrect syntax near (database name)

您好,我在 Java 程序中遇到此错误。这是我的查询。它在 SQL 服务器上运行良好。但是

Error: Incorrect syntax near 'WebApp'.

private static final String SERVICES = 
        "SELECT  s.Service_ID  "
            + ",s.[Location_ID]  "
            + ",COALESCE(st.[Service_Type_Name],s.[Service_Name]) AS Service_name "
            + ",st.Service_Type_Name "
            + " FROM    [WebApp].[dbo].[Services]  s join [WebApp].[dbo].[ServiceTypes]  st on s.Service_Type=st.Service_Type_ID "
            + " join WebApp.dbo.Locations l on s.Location_ID=l.Location_ID " 
            + " where s.Deleted=0 " 
            + " ORDER BY Location_ID ";

这是我的方法,它在 ms sql 服务器 2008

上运行良好
    public List<MAServiceVO> getAddServices() throws CoopCRSAPIException {
        ArrayList<MAServiceVO> results = new ArrayList<MAServiceVO>();
        MAServiceVO maServiceVO = null;
        log.debug("==========IN VendorDAOimpl.java (service)===========");
        //int serviceID = 0;
        //int prevServiceID = 0;
        try {
            conn = MSSQLDAOFactory.createConnection();
            stmt  = conn.prepareStatement(SERVICES);
//          stmt.setTimestamp(1, startDate);
//          stmt.setTimestamp(2, endDate);

            stmt.execute();
            rs = stmt.getResultSet();

            while (rs.next()) {

                    // create new service
                    maServiceVO = new MAServiceVO();
                    // set service fields
                    maServiceVO.setServiceID(rs.getInt("Service_ID"));
                    maServiceVO.setLocationID(rs.getInt("Location_ID"));                    
                    maServiceVO.setServiceName(rs.getString("Service_Name"));
                    maServiceVO.setServiceType(rs.getString("Service_Type_Name"));
                    log.debug("==========done with VendorDAOimpl.java (service)===========");

                } 

        } catch (SQLException e) {
            log.debug(e.getMessage());
            throw new CoopCRSAPIException(e.getMessage(), " VendorDAOimpl", "getAddServices", 500);
        } finally {
            closeConnections("getAddServices");
        }
        log.debug("&&&&&&&&&&&&&&&&&&&&&");
        log.debug("==========finsh===========");
        return results;

    }

我没有看到任何不正常的地方。如果有一个原因你没有在存储过程中而不是通过 sql?我确实注意到您没有在最终连接两边加上方括号,但这不会有任何区别。

这是您在删除 java.

的所有额外字符串部分后的查询
SELECT  s.Service_ID  
    , s.[Location_ID]  
    , COALESCE(st.[Service_Type_Name], s.[Service_Name]) AS Service_name 
    , st.Service_Type_Name 
FROM [WebApp].[dbo].[Services] s 
join [WebApp].[dbo].[ServiceTypes] st on s.Service_Type = st.Service_Type_ID 
join [WebApp].[dbo].[Locations] l on s.Location_ID = l.Location_ID  
where s.Deleted = 0  
ORDER BY Location_ID;