如果条件执行查询,否则根据要执行的计数执行其他查询

时间:2018-09-11 18:34:12

标签: sql sql-server if-statement select count

我想使用IFELSE条件执行不同的查询...

CASE WHEN (SELECT COUNT(*)
           FROM dcdetail AS d
           LEFT JOIN finalbilldetail AS f ON d.itemid = f.itemid
           WHERE ISNULL(d.itemcolorid, '') = ISNULL(f.itemcolorid,'')
           AND f.itemid IS NULL
           AND  d.id = 12) > 0 THEN
    SELECT dm.ID
          ,dm.Status
          ,dm.EntryDate
          ,dm.EntryTime
          ,ci.cusid
          ,ci.cusname
          ,ci.cuscontact
    FROM DcMaster AS dm
    LEFT OUTER JOIN RegCustomerInfo AS ci ON dm.customerID = ci.CusID
    WHERE dm.customerid = 8

ELSE 
    (Other Query)
END

1 个答案:

答案 0 :(得分:0)

在SQL Server CASE中不能用于控制流。请改用IF

IF (SELECT count(*)
           ...) > 0
BEGIN
  SELECT ...
END
ELSE
BEGIN
  SELECT ...
END;