如何将这个sql where子句组合使用一个Select?

时间:2014-02-07 20:35:50

标签: sql tsql

所以,如果你看一下最后的WHERE条款。 Where inv_location_id in其中一个我使用的是integer,另一个是set

我如何结合这些?

IF ( @inventory_location_id = NULL )
    SELECT  clinic_desc ,
            dbo.fn_get_clinic_address_formatted(@application_name,
                                                clinic_id, NULL, NULL,
                                                'STANDARD', NULL, NULL,
                                                NULL, NULL) AS formatted_address ,
            vfc_pin ,
            contact ,
            email_address ,
            phone ,
            fax
    FROM    dbo.clinics
    WHERE   clinic_id IN (
            SELECT  clinic_id
            FROM    dbo.clinic_inv_locations
            WHERE   inv_location_id IN (
                    SELECT  ilr.clinic_id
                    FROM    dbo.inv_location_reconciliation ilr
                    WHERE   inv_location_reconciliation_id = @reconciliation_id ) )

ELSE
    SELECT  clinic_desc ,
            dbo.fn_get_clinic_address_formatted(@application_name,
                                                clinic_id, NULL, NULL,
                                                'STANDARD', NULL, NULL,
                                                NULL, NULL) AS formatted_address ,
            vfc_pin ,
            contact ,
            email_address ,
            phone ,
            fax
    FROM    dbo.clinics
    WHERE   clinic_id IN (
            SELECT  clinic_id
            FROM    dbo.clinic_inv_locations
            WHERE   inv_location_id IN ( @inventory_location_id ) )

3 个答案:

答案 0 :(得分:2)

只需更改

的位置即可
WHERE (
    (
        @inventory_location_id IS NULL AND (
            inv_location_id IN (
            SELECT  ilr.clinic_id
            FROM    dbo.inv_location_reconciliation ilr
            WHERE   inv_location_reconciliation_id = @reconciliation_id )
        )
    )
    OR
    (
        @inventory_location_id IS NOT NULL AND (
            inv_location_id IN ( @inventory_location_id )
        )
    )
)

它可以更简洁,但为了清楚起见,我把它留下了大而且罗嗦:)

答案 1 :(得分:1)

我喜欢具有复杂查询的CTE,因为我认为它使它们更容易阅读,所以我会做类似下面的事情。还有很多其他方法可以解决这个问题,我并不认为这是最快的。

我没有测试过,所以请谨慎行事

with clinic_list AS
(
    SELECT  ilr.clinic_id as clinic_id  
    FROM    dbo.inv_location_reconciliation ilr
    WHERE   inv_location_reconciliation_id = @reconciliation_id 
    UNION ALL
    SELECT  clinic_id
    FROM    dbo.clinic_inv_locations
    WHERE   inv_location_id IN ( @inventory_location_id ) 
)
    SELECT  clinic_desc ,
            dbo.fn_get_clinic_address_formatted(@application_name,
                                                clinic_id, NULL, NULL,
                                                'STANDARD', NULL, NULL,
                                                NULL, NULL) AS formatted_address ,
            vfc_pin ,
            contact ,
            email_address ,
            phone ,
            fax
    FROM    dbo.clinics
    WHERE   clinic_id IN (SELECT  clinic_id FROM    clinic_list)

我也有强烈的怀疑,在这里使用函数是错误的选择。 SQL中的函数非常糟糕,通常是性能和扩展性差的原因。

答案 2 :(得分:0)

这就是我最终的目标

DECLARE @temp TABLE (clinic_id INT )
IF(@reconciliation_id IS NOT NULL)
  INSERT INTO @temp SELECT ilr.clinic_id 
                    FROM dbo.inv_location_reconciliation ilr 
                    WHERE inv_location_reconciliation_id =@reconciliation_id
ELSE
  INSERT INTO @temp (clinic_id) VALUES (@inventory_location_id)
SELECT clinic_desc, dbo.fn_get_clinic_address_formatted(@application_name, clinic_id,      NULL, NULL, 'STANDARD', null,NULL, NULL, NULL) AS formatted_address,vfc_pin, contact, email_address, phone, fax 
FROM dbo.clinics 
WHERE clinic_id IN (SELECT clinic_id 
                    FROM dbo.clinic_inv_locations 
                    WHERE inv_location_id in (SELECT clinic_id FROM @temp))