是否可以设置局部变量以允许任何值?

时间:2015-10-22 20:32:40

标签: sql-server

我有一系列用于质量检查的查询。通常我们将organizationid限制为单个值,但有时我们希望返回所有organizationid。是否可以在不必在查询中注释掉所有@orgid的情况下执行此操作?

示例:

DECLARE @REPSD DATETIME = '10-01-2014';
DECLARE @REPED DATETIME = '09-30-2015';
DECLARE @orgid BIGINT = 5

SELECT
    patientid,
    MAX (dateofobservation) sbp_date INTO #t1
FROM
    dbo.patientobservation (nolock)
WHERE observationcode IN ('8479-8','8480-6','8459-0','8460-8','8461-6')
    AND dateofobservation >= @REPSD
    AND dateofobservation <= @REPED 
    AND organizationid = @orgid

1 个答案:

答案 0 :(得分:4)

可能你想要:

onChange: function(option, checked, select) {
    var opselected = $(option).val();
    if(checked == true) {
        if (opselected == '1') { $(".toggleG").toggle(); }
        if (opselected == '2') { $(".toggleE").toggle(); }
        if (opselected == '3') {
            $(".toggleLAT").toggle();
            $(".toggleLON").toggle();
        }
        if (opselected == '4') { $(".toggleLN").toggle(); }
        if (opselected == '5') { $(".toggleTOD").toggle(); } 
    } else if(checked == false)
        if (opselected == '1') { $(".toggleG").toggle(); }
        if (opselected == '2') { $(".toggleE").toggle(); }
        if (opselected == '3') {
            $(".toggleLAT").toggle();
            $(".toggleLON").toggle();
        }
        if (opselected == '4') { $(".toggleLN").toggle(); }
        if (opselected == '5') { $(".toggleTOD").toggle(); }
    }
}

或:

DECLARE @REPSD DATETIME = '10-01-2014';
DECLARE @REPED DATETIME = '09-30-2015';
DECLARE @orgid BIGINT = NULL

SELECT
    patientid,
    MAX (dateofobservation) sbp_date INTO #t1
FROM
    dbo.patientobservation (nolock)
WHERE observationcode IN ('8479-8','8480-6','8459-0','8460-8','8461-6')
    AND dateofobservation >= @REPSD
    AND dateofobservation <= @REPED 
    AND organizationid = ISNULL(@orgid,organizationid);

使用SELECT patientid, MAX (dateofobservation) sbp_date INTO #t1 FROM dbo.patientobservation (nolock) WHERE observationcode IN ('8479-8','8480-6','8459-0','8460-8','8461-6') AND dateofobservation >= @REPSD AND dateofobservation <= @REPED AND ( (organizationid = @orgid AND @orgid IS NOT NULL) OR @orgid IS NULL ) 建议相当于上述但更简单,更紧凑:

JamieD77

您应该考虑使用第三个示例,因为第一个示例不是-SARGable,因此查询优化器如果存在任何索引,则不会使用该列上的索引。