Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of SQL_CALC_FOUND_ROWS in SQL Server?

I have this mssql query:

with RESULT as(select  TITLE, URL, ROW_NUMBER() over (order by URL) as SeqValue from WEBSITE 
select * from RESULT where SeqValue>=20 and SeqValue<=40 

I'd like to know how many record this query would return if the where statement was not there. I try with select count(*) from RESULT and try with @@ROWCOUNT and many others way but did not work. I need TITLE and URL from select, and in the end i need total record for the select.

For example in mysql query i have prepareStatement using SQL_CALC_FOUND_ROWS:

select SQL_CALC_FOUND_ROWS TITLE, URL from WEBSITE limit ?, ?

and after this select i have:

select FOUND_ROWS()

In this example returned value is total record for the mysql query. Total record is same with LIMIT and without LIMIT directive. I convert database from mysql to mssql and i have problem with this. Please help me...

like image 804
user1288792 Avatar asked Apr 15 '13 21:04

user1288792


People also ask

Is Sql_calc_found_rows deprecated?

F1. When writing a query with SQL_CALC_FOUND_ROWS or FOUND_ROWS(), there should be a warning (both with the standard deprecation warning code 1287): Warning 1287 SQL_CALC_FOUND_ROWS is deprecated and will be removed in a future release. Consider using two separate queries instead.

Why use SQL_ CALC_ FOUND_ ROWS?

SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again.

What is Row_count () in MySQL?

ROW_COUNT() returns the number of rows updated, inserted or deleted by the preceding statement. This is the same as the row count that the mysql client displays and the value from the mysql_affected_rows() C API function.

Is SQL Server between inclusive?

The SQL BETWEEN Operator The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.


1 Answers

I had the same concern when trying to move from MySQL to SQL SERVER the solution is to inject this in your query:

COUNT (*) OVER () AS ROW_COUNT

ROWCOUNT appear in all rows in the result then it only remains for you to retrieve ROW_COUNT from the first row result (if exists) and not forget to reset the result pointer et Voila;-).

for example:

select COUNT (*) OVER () AS ROW_COUNT, URL from WEBSITE limit ?, ?

I hope it will help

like image 170
ghani Avatar answered Oct 12 '22 06:10

ghani