SQL Server存储过程:如果@variable不是null col2,则选择col1

时间:2014-11-25 10:41:34

标签: sql-server stored-procedures

我有一个SQL Server存储过程,我想扩展它以包含距离搜索。

SELECT col1, col2, col3, col4
FROM table
WHERE ......

存储过程包含@Longitude和@Latitude的变量。如果这些已经传入,我想返回一个距离,否则返回9999999.我试过了:

SELECT col1, col2, col3, col4,
IF @Latitude IS NOT NULL AND @Longitude IS NOT NULL 
                3956 * 2 * ASIN(
                  SQRT( POWER(SIN((@Latitude - abs(dest.Latitude)) * pi()/180 / 2), 2) 
                      + COS(@Longitude * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180)  
                      * POWER(SIN((@Longitude - dest.Longitude) * pi()/180 / 2), 2) )) 
                  AS Distance
            ELSE
                9999999 AS Distance
FROM table
WHERE ......

但我正在

  

关键字“IF”附近的语法不正确

我试图做到的是可以实现的吗?我是否需要以某种方式将IF与SELECT语句的主体分开? (如果这是一个基本问题,请道歉)

由于

4 个答案:

答案 0 :(得分:3)

您需要使用CASE

SELECT col1, col2, col3, col4,
CASE WHEN @Latitude IS NOT NULL AND @Longitude IS NOT NULL THEN
                3956 * 2 * ASIN(
                  SQRT( POWER(SIN((@Latitude - abs(dest.Latitude)) * pi()/180 / 2), 2) 
                      + COS(@Longitude * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180)  
                      * POWER(SIN((@Longitude - dest.Longitude) * pi()/180 / 2), 2) )) 
            ELSE
                9999999 
END AS Distance
FROM table
WHERE ......

答案 1 :(得分:2)

CASE查询

中使用IF代替SELECT
    SELECT col1, col2, col3, col4,
              CASE WHEN( @Latitude IS NOT NULL AND @Longitude IS NOT NULL )
              THEN
                3956 * 2 * ASIN(
                  SQRT( POWER(SIN((@Latitude - abs(dest.Latitude)) * pi()/180 / 2), 2) 
                      + COS(@Longitude * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180)  
                      * POWER(SIN((@Longitude - dest.Longitude) * pi()/180 / 2), 2) )) 

            ELSE
                9999999 
            END AS Distance

FROM table
WHERE ......

答案 2 :(得分:1)

在SQL中执行操作的正确方法是使用CASE WHEN

示例:

DECLARE @int INT = 1

SELECT  col1 ,
        CASE WHEN @int = 1 THEN 'Yes'
             ELSE 'No'
        END AS YN
FROM    table

<强> Reference: CASE WHEN

IF语法可用于函数和存储过程:

示例:

DECLARE @int INT = 1

IF @int = 1 
    BEGIN
        SELECT  col1 , 'Yes' AS YN
        FROM    table
    END
ELSE
    BEGIN
        SELECT  col1 , 'No' AS YN
        FROM    table
    END

<强> Reference: IF.. ELSE

答案 3 :(得分:0)

试试这个。

SELECT 
 [col1],
 [col2],
 [col3],
 [col4],
    CASE 
        WHEN 
            @Latitude IS NOT NULL AND 
            @Longitude IS NOT NULL 
        THEN
            3956 * 2 * ASIN(SQRT( POWER(SIN((@Latitude - abs(dest.Latitude)) * pi()/180 / 2), 2) 
                      + COS(@Longitude * pi()/180 ) 
                      * COS(abs(dest.Latitude) * pi()/180)  
                      * POWER(SIN((@Longitude - dest.Longitude) * pi()/180 / 2), 2) )) 
        ELSE
             9999999 
    END AS [Distance]
FROM TableName
WHERE ......

2012年使用IIf进行比较的例子:

  

IF -1&lt; 1那么“正确”错误'错误'

您可以按如下方式使用CASE语句:

-- SQL Server 2008 and earlier version solution

SELECT CASE
WHEN -1 < 1 THEN 'TRUE'
ELSE 'FALSE' END AS Result

GO

-- SQL Server 2012 solution

SELECT IIF ( -1 < 1, 'TRUE', 'FALSE' ) AS Result;

GO

请参阅更多信息

  

官方 - http://msdn.microsoft.com/en-IN/library/hh213574.aspx

     

有用 - http://blog.sqlauthority.com/2011/09/10/sql-server-denali-logical-function-iif-a-quick-introduction/