Need a row count after SELECT statement: what's the optimal SQL approach?

后端 未结 10 1090
悲哀的现实
悲哀的现实 2020-12-05 02:19

I\'m trying to select a column from a single table (no joins) and I need the count of the number of rows, ideally before I begin retrieving the rows. I have come to two app

10条回答
  •  星月不相逢
    2020-12-05 02:39

    If you're concerned the number of rows that meet the condition may change in the few milliseconds since execution of the query and retrieval of results, you could/should execute the queries inside a transaction:

    BEGIN TRAN bogus
    
    SELECT COUNT( my_table.my_col ) AS row_count
    FROM my_table
    WHERE my_table.foo = 'bar'
    
    SELECT my_table.my_col
    FROM my_table
    WHERE my_table.foo = 'bar'
    ROLLBACK TRAN bogus
    

    This would return the correct values, always.

    Furthermore, if you're using SQL Server, you can use @@ROWCOUNT to get the number of rows affected by last statement, and redirect the output of real query to a temp table or table variable, so you can return everything altogether, and no need of a transaction:

    DECLARE @dummy INT
    
    SELECT my_table.my_col
    INTO #temp_table
    FROM my_table
    WHERE my_table.foo = 'bar'
    
    SET @dummy=@@ROWCOUNT
    SELECT @dummy, * FROM #temp_table
    

提交回复
热议问题