错误:语法不正确(数据库名称)

时间:2015-01-20 22:12:06

标签: java sql-server jdbc

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

  

错误: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 server 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;

    }

1 个答案:

答案 0 :(得分:0)

我在那里看不到什么东西。如果有理由你没有在存储过程中有这个而不是通过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;