使用" UNION"与"如果存在"

时间:2015-08-03 19:51:01

标签: sql sql-server sql-server-2008-r2

我在SQL Server 2008 R2上遇到以下问题,我试图在IF EXISTS组中使用UNION子句,并且我收到错误。

如果我将查询分开运行,它们运行时没有错误,但是如果我尝试使用UNION运行它们,它会给我错误:

  

第15行,第15行,第1行,第9行   关键字' IF'附近的语法不正确。

SELECT
    '1.5.2- Customers' as INPUT,
    count (distinct ID) as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
FROM
    [BASE]
WHERE
    BASE.CAT_CUSTO = 'EMPLO' and
    BASE.ESTAT = 'EX'
group by
    PRO_DATE

UNION 

IF  EXISTS (
    SELECT
        '1.6- OTHER CUSTOMERS' as INPUT,
        count (distinct ID)  as Z,
        PRO_DATE,
        Month(PRO_DATE) as P_MONTH,
        Year(PRO_DATE) as P_YEAR
    FROM
        [BASE]
    WHERE
        BASE.CAT_CUSTO <> 'EMPLO'
    group by
        PRO_DATE
)  begin
SELECT
        '1.6- OTHER CUSTOMERS' as INPUT,
        count (distinct ID)  as Z,
        PRO_DATE,
        Month(PRO_DATE) as P_MONTH,
        Year(PRO_DATE) as P_YEAR
    FROM
        [BASE]
    WHERE
        BASE.CAT_CUSTO <> 'EMPLO'
    group by
        PRO_DATE

 end
ELSE
    SELECT TOP 1
        '1.6- OTHER CUSTOMERS' as INPUT,
        0 as Z,
        PRO_DATE,
        Month(PRO_DATE) as P_MONTH,
        Year(PRO_DATE) as P_YEAR
    FROM
        [BASE]

3 个答案:

答案 0 :(得分:2)

你说

    function bindInfoWindow(marker, map, infoWindow) {
        var infowindow = new google.maps.InfoWindow({});
        google.maps.event.addListener(marker, 'click', function() {
            infoWindow.content=marker.html[0];
            infoWindow.open(map, marker);
            console.log(html)
            window.localStorage.setItem("html",html);
        });
    }

不起作用,但所有查询都有效。我怀疑它在查看查询,但如果是这种情况,那么只需将其更改为

q1

union 

if exists(q2)

  q3

else

  q4

答案 1 :(得分:0)

您可以使用临时表:

SELECT
    '1.6- OTHER CUSTOMERS' as INPUT,
    count (distinct ID)  as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
  INTO #temp
FROM
    [BASE]
WHERE
    BASE.CAT_CUSTO <> 'EMPLO'
group by
    PRO_DATE

然后

IF SELECT COUNT(*) FROM #temp > 0
BEGIN
SELECT
    '1.5.2- Customers' as INPUT,
    count (distinct ID) as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
FROM
    [BASE]
WHERE
    BASE.CAT_CUSTO = 'EMPLO' and
    BASE.ESTAT = 'EX'
group by
    PRO_DATE

UNION 

SELECT
    '1.6- OTHER CUSTOMERS' as INPUT,
    count (distinct ID)  as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
FROM
    [BASE]
WHERE
    BASE.CAT_CUSTO <> 'EMPLO'
group by
    PRO_DATE

 end

ELSE

SELECT
    '1.5.2- Customers' as INPUT,
    count (distinct ID) as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
FROM
    [BASE]
WHERE
    BASE.CAT_CUSTO = 'EMPLO' and
    BASE.ESTAT = 'EX'
group by
    PRO_DATE

UNION 

SELECT TOP 1
    '1.6- OTHER CUSTOMERS' as INPUT,
    0 as Z,
    PRO_DATE,
    Month(PRO_DATE) as P_MONTH,
    Year(PRO_DATE) as P_YEAR
FROM
    [BASE]

答案 2 :(得分:0)

DECLARE @chk NVARCHAR(1)='B';
/** change @chk as 'C' to see the result**/
WITH T1 AS(SELECT * FROM(VALUES('A',1))a(F1,F2)
),T2 AS(SELECT * FROM(VALUES('B',2))a(F1,F2)
),T3 AS(SELECT * FROM(VALUES('C',3))a(F1,F2)
),CHK AS(SELECT CASE WHEN EXISTS(SELECT * FROM T2 WHERE F1=@chk) THEN 'B' ELSE 'C' END[chk_f]
) SELECT * FROM T1 UNION SELECT T2.* FROM T2,CHK WHERE chk_f='B' UNION SELECT T3.* FROM T3,CHK WHERE chk_f='C'