2012-08-09 31 views
0

我在選擇語句下運行,並100%確定第二行導致問題,因爲子查詢運行良好。SQL服務器選擇子查詢錯誤

Where (first,second) IN有效聲明?

我正在做這個查詢的原因是,我將然後用刪除替換第一個選擇,所以我只是想確保我得到我想要的設置。

select * from edit_proj_isbn e 
where (e.record_id, e.proj_isbn) 
IN 
(
    select t_r.record_id, t_r.proj_isbn from editorial e, 
    (
     select ed.record_id as record_id, substring(d.file_name, 1, 13) as epubisbn, epi.proj_isbn as proj_isbn, ed.asset_subtype as asset_subtype 
     from editorial ed join doc_renditions d on ed.record_id = d.record_id join edit_proj_isbn epi on ed.record_id = epi.record_id 
     where (ed.asset_subtype like 'epub' or ed.asset_subtype like 'updf') 
     and substring(d.file_name, 1, 13) not like epi.proj_isbn 
    ) AS t_r 
    where t_r.record_id=e.record_id 
) 

我得到以下錯誤,與第二行強調:

消息4145,級別15,狀態1,行2
在上下文,其中指定的非布爾類型的表達式一個條件是預計的,靠近','。

感謝, 布魯斯

+0

你可能不希望使用表的別名'e'兩次。它可能會令人困惑。 – HABO 2012-08-09 18:21:19

回答

2

就是(第一,第二),在一個有效的語句?

不,這不是一個有效的陳述。你需要爲每一個單獨的WHERE條款。

+0

這不是MSSQL中的有效聲明*。 – Andreas 2016-06-10 08:02:42

+0

@Andreas考慮隱含的帶標記的SQL Server。 – Taryn 2016-06-10 11:25:40

0

快速贓物使用CTE:

with Timmy as (
    select t_r.record_id, t_r.proj_isbn from editorial e, 
    ( 
    select ed.record_id as record_id, substring(d.file_name, 1, 13) as epubisbn, epi.proj_isbn as proj_isbn, ed.asset_subtype as asset_subtype 
    from editorial ed join doc_renditions d on ed.record_id = d.record_id join edit_proj_isbn epi on ed.record_id = epi.record_id 
    where (ed.asset_subtype like 'epub' or ed.asset_subtype like 'updf') 
    and substring(d.file_name, 1, 13) not like epi.proj_isbn 
) AS t_r 
where t_r.record_id=e.record_id) 
select * from edit_proj_isbn 
    where record_id in (select t_r.record_id from Timmy) and proj_isbn in (select t_r.proj_isbn from Timmy)