开发者

keep getting invalid column name bookings when that is my table name not my column name

开发者 https://www.devze.com 2023-03-07 20:56 出处:网络
SELECT * FROM ( SELECT abcfiles.dbo.diary.BOOKNO, abcfiles.dbo.BOOKINGS.PARTY, abcfiles.dbo.BOOKINGS.DEPART,
SELECT
    *
FROM
(
    SELECT
        abcfiles.dbo.diary.BOOKNO,
        abcfiles.dbo.BOOKINGS.PARTY,
        abcfiles.dbo.BOOKINGS.DEPART,
        abcfiles.dbo.BOOKINGS.DEP7,
        abcfiles.dbo.BOOKINGS.BOOKED,
        abcfiles.dbo.BOOKINGS.BOOK7,
        abcfiles.dbo.BOOKINGS.APRICE,
        abcfiles.dbo.BOOKINGS.PAID,
        abcfiles.dbo.DIARY.ADDED,
        abcfiles.dbo.DIARY.ADDED7,
        abcfiles.dbo.DIARY.TEXT,
        ROW_NUMBER() OVER(PARTITION BY abcfiles.dbo.BOOKINGS开发者_Go百科.BOOKNO ORDER BY abcfiles.dbo.diary.ADDED DESC) AS RowNum
    FROM
        abcfiles.dbo.BOOKINGS
    INNER JOIN abcfiles.dbo.DIARY ON abcfiles.dbo.BOOKINGS.BOOKNO = abcfiles.dbo.DIARY.BOOKNO
    WHERE
    (
        abcfiles.dbo.BOOKINGS.PAID IS NULL
    ) OR
    (
        abcfiles.dbo.BOOKINGS.PAID = 0
    )
) t     
WHERE
    t.RowNum = 1 
END

The reason that I keep writing abcfiles is because I need my stored procedure stored in a different database.


Use aliases (t was already an alias).

SELECT * FROM (
    SELECT
        b.BOOKNO, b.PARTY, b.DEPART, b.DEP7, b.BOOKED, b.BOOK7, b.APRICE, b.PAID,
        d.ADDED, d.ADDED7, d.TEXT,
        ROW_NUMBER() OVER(PARTITION BY b.BOOKNO ORDER BY d.ADDED DESC) AS RowNum      
     FROM abcfiles.dbo.BOOKINGS as b
    INNER JOIN abcfiles.dbo.DIARY as d ON b.BOOKNO = d.BOOKNO
    WHERE (b.PAID IS NULL)
       OR (b.PAID = 0)) t     
WHERE t.RowNum = 1 

The as keyword is optional.

Happy SQL'ing.

0

精彩评论

暂无评论...
验证码 换一张
取 消