sql server在UNION之前使用IF语句

时间:2017-11-13 12:54:51

标签: sql sql-server

我正在添加一个UNION语句,我需要为UNION添加一个IF语句,但是我得到一个“IF'附近的语法错误。期待')',EXCEPT或SELECT”错误。

为了提高处理器效率,如果变量字符串中有特殊字符,我只想调用UNION。这是我的选择声明。任何帮助表示赞赏。感谢。

set @DelimitedKeyWord = ( select replace(@KeyWord, '-', '') ) 
set @DelimiterExists = ( select CHARINDEX('-', @KeyWord) ) 

select distinct top (@MaxHits) 
    results.MYID as ItemId
  , results.Rank0 as [RANK]
from (
  select 
      [MYID]
    , [RANK] as Rank0
  from [rcl].[MyTable] rm
    inner join CONTAINSTABLE([MyTable], *, @KeyWord) as CNT 
      on CNT.[Key] = rm.RecallID 

  if (@DelimiterExists > 0)
    begin
      union
      select 
          [MYID]
        , [RANK] as Rank0
      from [rcl].[MyTable] rm
      inner join CONTAINSTABLE([MyTable], *, @DelimitedKeyWord) as DCNT 
        on DCNT.[Key] = rm.RecallID
    end
  ) as results

2 个答案:

答案 0 :(得分:0)

SQL并不像那样工作。一种方法是在WHERE子句中包含条件:

SELECT DISTINCT TOP(@MaxHits) results.MYID AS ItemId, results.Rank0 AS [RANK]
FROM ((SELECT [MYID], [RANK] AS Rank0
       FROM [rcl].[MyTable] rm JOIN
            CONTAINSTABLE ([MyTable], *, @KeyWord) AS CNT
            ON CNT.[Key] = rm.RecallID
      )
      UNION ALL
      (SELECT [MYID], [RANK] AS Rank0
       FROM [rcl].[MyTable] rm JOIN
            CONTAINSTABLE ([MyTable], *, @DelimitedKeyWord) AS DCNT
            ON DCNT.[Key] = rm.RecallID
       WHERE @DelimiterExists > 0
      )
     ) x;

注意:使用UNION ALL。您已在外部查询中删除重复项。无需两次这样做。

答案 1 :(得分:0)

这可行吗?

set @DelimitedKeyWord = (select replace(@KeyWord, '-', ''))
             set @DelimiterExists = (select CHARINDEX('-', @KeyWord))

IF (@DelimiterExists > 0)

    BEGIN
        SELECT DISTINCT TOP(@MaxHits)
                    results.MYID AS ItemId,
                    results.Rank0 AS [RANK]
                    FROM
                           (
                                 SELECT
                                        [MYID],
                                        [RANK] AS Rank0
                                 FROM  [rcl].[MyTable] rm
                                        JOIN CONTAINSTABLE ([MyTable], *, @KeyWord) AS CNT
                                               ON CNT.[Key] = rm.RecallID




                                 UNION ALL

                                 SELECT
                                        [MYID],
                                        [RANK] AS Rank0
                                        FROM  [rcl].[MyTable] rm

                                               JOIN CONTAINSTABLE ([MyTable], *, @DelimitedKeyWord) AS DCNT ON DCNT.[Key] = rm.RecallID    

                                 ) AS results

    END

ELSE

    BEGIN

    SELECT DISTINCT TOP(@MaxHits)
            results.MYID AS ItemId,
            results.Rank0 AS [RANK]
            FROM
                   (
                         SELECT
                                [MYID],
                                [RANK] AS Rank0
                         FROM  [rcl].[MyTable] rm
                                JOIN CONTAINSTABLE ([MyTable], *, @KeyWord) AS CNT
                                       ON CNT.[Key] = rm.RecallID
                     ) AS results

    END