简体   繁体   中英

MSSQL - Invalid column name with Java/JTDS and Join

I want to execute the following statement using Java with JTDS in MSSQL 2005. I need it that way, because I want to show the results from position 1-15, 16-30 etc.

SELECT TOP 15 WID,AID,de FROM 
(SELECT WID,AID, de, ROW_NUMBER() OVER (ORDER BY WID ASC) AS r_n_n 
FROM [database].[dbo].[table1], [database].[dbo].[table2] 
WHERE AID=Indicator) x WHERE r_n_n >= 1

When I copy this code in the Microsoft SQL Management-Studio and exceute it, it shows the expected results. When excecuting it in Java with

        s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = s.executeQuery(sql);

it says "Invalid column name 'Indicator'". WID and AID are from table1. de and Indicator are from table 2. I made a couple of other queries with Java and JDTS which all worked fine. Only the that query with the Join does not work. I think it has something to do with this, but I don't know what.

Anyone has an idea? Thanks in advance and best regards.

Edit: Currently I use JTDS 1.2.5, but I also tried 1.3.0 which doesn't make a difference.

If Indicator and de are from Table2 write so in the query. Don't assume it is implied. Also you should write Joins the way they are supposed to be written - using JOIN clause and not in the 'old-style' implicit way.

Also you must add ORDER BY if you are using TOP otherwise you won't know what 15 rows will you get.

Not sure if it will help you, but try (something like) this:

SELECT TOP 15 WID,AID,de FROM 
(
    SELECT t1.WID, t1.AID, t2.de, ROW_NUMBER() OVER (ORDER BY t1.WID ASC) AS r_n_n 
    FROM [database].[dbo].[table1] t1
    INNER JOIN [database].[dbo].[table2] t2 ON t1.AID = t2.INDICATOR
) x 
WHERE r_n_n >= 1
ORDER BY r_n_n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM