SELECT *从具有IF EXISTS的子查询中

时间:2018-08-23 19:50:07

标签: sql sql-server tsql

似乎无法弄清楚。

OrganizationUserSettings有两行,其中SettingSys3,而OrganizationSys1012

总是行,其中OrgUserSettingsProfileSys为空。 可能在一行(或多行)中具有值。

给出一个参数@OrgUserSettingsProfileSys,我需要一个与之匹配的行(如果存在)。如果没有,那么我需要在其中为空的行。

enter image description here

SELECT * FROM
(
    IF EXISTS (Select * FROM OrganizationUserSettings WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys)
    BEGIN 
        SELECT * FROM OrganizationUserSettings WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys
    END
    ELSE
    BEGIN
        SELECT * FROM OrganizationUserSettings WHERE OrgUserSettingsProfileSys IS NULL
    END
)
WHERE SettingSys = 3 AND OrganizationSys = 1012

有了这个,我得到

  

'IF'附近的语法不正确

编辑:

为了在此处发布,我简化了此查询。我需要将它作为子查询的一部分。

6 个答案:

答案 0 :(得分:3)

 SELECT top 1 * 
FROM OrganizationUserSettings 
WHERE SettingSys = 3 AND OrganizationSys = 1012
and (OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys or OrgUserSettingsProfileSys is NULL)
order by OrgUserSettingsProfileSys desc

答案 1 :(得分:3)

如果有多行不为空,则可以将top 1与领带一起使用以获得多于一行

select      top 1 with ties
            *
from        OrganizationUserSettings
where       SettingSys = 3
and         OrganizationSys = 1012
and         (   OrgUserSettingsProfileSys is null
          or    OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys)
order by    case
                when OrgUserSettingsProfileSys is null then 2
            else 1
            end

答案 2 :(得分:2)

您不能在这样的查询中放入IF

您可以做自己想做的事情的一种方法是:

IF EXISTS(SomeQuery)
   SELECT Query A
ELSE
   SELECT Query B

进行子查询以执行您想要执行的操作的一种方法是:

SELECT * FROM (
  SELECT * FROM MyTable WHERE SomeColumn IS NOT NULL AND OtherConditions
  UNION ALL
  SELECT * FROM MyTable 
  WHERE SomeColumn IS NULL AND OtherConditions
  AND NOT EXISTS ( SELECT * FROM MyTable WHERE SomeColumn IS NOT NULL AND OtherConditions)
) sq

答案 3 :(得分:2)

这是一个奇怪的查询。也许您打算:

WITH t as (
      SELECT *
      FROM OrganizationUserSettings
      WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys
     )
SELECT *
FROM ((SELECT t.*
       FROM t
      ) UNION ALL
      (SELECT *
       FROM OrganizationUserSettings
       WHERE OrgUserSettingsProfileSys IS NULL AND
             NOT EXISTS (SELECT 1 FROM t)
      )
     ) t
WHERE SettingSys = 3 AND OrganizationSys = 1012;

如果您期望只有一行匹配,那么可以更简单地写为:

WITH t as (
      SELECT TOP (1) *
      FROM OrganizationUserSettings
      WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys OR
            OrgUserSettingsProfileSys IS NULL
      ORDER BY (CASE WHEN OrgUserSettingsProfileSys IS NOT NULL THEN 1 ELSE 2 END)
     )
SELECT *
FROM t
WHERE SettingSys = 3 AND OrganizationSys = 1012;

答案 4 :(得分:2)

这应该有效:

   IF EXISTS
(
    SELECT *
    FROM OrganizationUserSettings
    WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys
)
BEGIN
    SELECT *
    FROM
    (
        SELECT *
        FROM OrganizationUserSettings
        WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys
    ) AS x
    WHERE SettingSys = 3
          AND OrganizationSys = 1012;

END;
ELSE
BEGIN
    SELECT *
    FROM
    (
        SELECT *
        FROM OrganizationUserSettings
        WHERE OrgUserSettingsProfileSys IS NULL
    ) AS x
    WHERE SettingSys = 3
          AND OrganizationSys = 1012;
END;

答案 5 :(得分:1)

一个简单的OR应该可以工作,要么检查该值是否存在并且该行具有该值,要么检查该值不存在并且该行具有空值。

SELECT *
       FROM OrganizationUserSettings
       WHERE (EXISTS (SELECT *
                             FROM OrganizationUserSettings
                             WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys)
              AND OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys
               OR NOT EXISTS (SELECT *
                                     FROM OrganizationUserSettings
                                     WHERE OrgUserSettingsProfileSys = @OrgUserSettingsProfileSys)
                  AND OrgUserSettingsProfileSys IS NULL)
             AND SettingSys = 3
             AND OrganizationSys = 1012;