查找sql​​查询的最简单方法是返回结果与否?

时间:2010-04-09 05:38:08

标签: sql-server-2005

考虑以下sql server查询,

 DECLARE @Table TABLE(
        Wages FLOAT
)

INSERT INTO @Table SELECT 20000 
INSERT INTO @Table SELECT 15000 
INSERT INTO @Table SELECT 10000
INSERT INTO @Table SELECT 45000
INSERT INTO @Table SELECT 50000

SELECT  *
FROM    (
            SELECT  *,
                    ROW_NUMBER() OVER(ORDER BY Wages DESC) RowID
            FROM    @Table 
        ) sub
WHERE   RowID = 3

查询的结果将是20000 .....就像现在一样,我必须找到此查询的结果,

SELECT  *
FROM    (
            SELECT  *,
                    ROW_NUMBER() OVER(ORDER BY Wages DESC) RowID
            FROM    @Table 
        ) sub
WHERE   RowID = 6

它不会给出任何结果,因为表中只有5行..... 所以现在我的问题是

找到sql查询的最简单方法是返回结果?

2 个答案:

答案 0 :(得分:2)

使用@@ROWCOUNT > 0

所以作为一个简单的例子,

SELECT  *
FROM    (
            SELECT  *,
                    ROW_NUMBER() OVER(ORDER BY Wages DESC) RowID
            FROM    @Table 
        ) sub
WHERE   RowID = 6

IF @@ROWCOUNT > 0 BEGIN
    RETURN 1
END
ELSE BEGIN
    RETURN 0
END

有关详细信息,请参阅文档here's a link

答案 1 :(得分:0)

像这样:

SELECT @@rowcount