将查询从Oracle转换为SQL

时间:2012-05-03 11:26:41

标签: oracle sql-server-2008

我的查询在Oracle上运行得非常快。

field1field2以及field3都是整数

select * FROM MY_TABLE P WHERE   
USER_ID = 167 AND 
(field1,field2,field3) NOT IN 
( SELECT field1,field2,field3 FROM EXCLUSION_TABLE)

我将其翻译成T / SQL(SQL Server 2008R2)

select * FROM MY_TABLE P WHERE   
USER_ID = 167 AND 
(cast(field1 as varchar)+ ','+cast(field2 as varchar)+ ','+
cast(field3 as varchar)) 
NOT IN 
( SELECT cast(field1 as varchar)+ ','+cast(field2 as varchar)+ ','+
cast(field3 as    varchar) FROM EXCLUSION_TABLE)

有没有人有任何想法我怎么能以不同的方式做到这一点?

谢谢。

1 个答案:

答案 0 :(得分:3)

也许是这样的:

select * 
FROM MY_TABLE P 
WHERE USER_ID = 167 
AND NOT EXISTS
(
    SELECT
        NULL
    FROM
        EXCLUSION_TABLE
    WHERE
        EXCLUSION_TABLE.field1=P.field1
        AND EXCLUSION_TABLE.field2=P.field2
        AND EXCLUSION_TABLE.field3=P.field3
)

你可以在oracle中做同样的事情