1

我有类似的东西 -

IF EXISTS (SELECT 1 FROM systable st
                JOIN sysuserperm sup ON st.creator = sup.user_id
                WHERE sup.user_name = 'TEST'
                AND st.table_type = 'BASE'
                AND st.table_name = 'Table1') THEN
IF EXISTS (SELECT 1 FROM systable st
                JOIN sysuserperm sup ON st.creator = sup.user_id
                WHERE sup.user_name = 'TEST'
                AND st.table_type = 'BASE'
                AND st.table_name = 'Table2') THEN

将多个这些组合在一个存在中的最佳方法是什么?

4

3 回答 3

3

试试这个,

SELECT  1 
FROM    systable st
        INNER JOIN sysuserperm sup 
            ON st.creator = sup.user_id
WHERE   sup.user_name = 'TEST' AND 
        st.table_type = 'BASE' AND 
        st.table_name IN ('Table1', 'table2')
GROUP   BY sup.user_name, st.table_type
HAVING  COUNT(DISTINCT st.table_name) = 2
于 2013-09-20T19:55:41.290 回答
1

这应该完成任务

IF EXISTS (
    (SELECT 1 FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME='TABLE1') 
    INTERSECT
    (SELECT 1 FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME='TABLE2')
)
于 2014-06-06T07:20:27.120 回答
0

更多行会更快:

IF EXISTS (SELECT TOP 1 1 FROM systable st
                JOIN sysuserperm sup ON st.creator = sup.user_id
                WHERE sup.user_name = 'TEST'
                AND st.table_type = 'BASE'
                AND st.table_name in ('Table1', 'Table2')) THEN
于 2013-09-20T20:14:09.233 回答