Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Maximum number of records to return

We have RowCount to set number of records to return or affect. But we need to set this each time before executing sql statement. I am looking a way to set this once my application started and so it affect all queries execute via my application.

Update

I want to achieve a trial version strategy with limited records! I don't want to mess with my actual code and wish somehow database restrict number of records return in result of any query! An alternative could be pass a parameter to each store-procedure but I really don't like this one and looking for some other strategy!

like image 785
mqpasta Avatar asked Jun 08 '11 11:06

mqpasta


People also ask

How many records can one SQL return in one transaction?

This cumulative limit is 11 times the per-namespace limit. For example, if the per-namespace limit for SOQL queries is 100, a single transaction can perform up to 1,100 SOQL queries. In this case, the cumulative limit is 11 times the per-namespace limit of 100.

Which keyword sets the maximum number of records to return?

The LIMIT keyword of is used to limit the number of rows returned from a result set.

What does limit 1 1 do in SQL?

In this syntax, the LIMIT n-1, 1 clause returns 1 row that starts at the row n. For example, the following query returns the employee information who has the second-highest income: SELECT emp_name, city, income FROM employees.

Does SQL Server have a limit?

TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results. The SELECT TOP statement is Microsoft's proprietary equivalent to the SELECT LIMIT statement.


2 Answers

You can parameterise TOP in your code for all commands

DECLARE @rows int
SET @row = ISNULL(@row, 2000000000)
SELECT TOP (@rows) ... FROM ..

You can have TOP defined globally for all your queries this way, say by wrapping or extending SQLCommand

SET ROWCOUNT is not a safe option: it affects intermediate result sets and has other unpredictable behaviour. And it's partially deprecated to ignored in I/U/D DML: see MSDN

Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in the next release of SQL Server

like image 92
gbn Avatar answered Sep 28 '22 22:09

gbn


You don't mention the application language you are using, so I can't give you the code, but make a wrapper function for a database connect. In this function, after you connect to the database, issue the SET ROWCOUNT n, make n a parameter and add any logic you need to make it variable. I do a similar thing with CONTEXT_INFO, I set it in my connection function.

Remember, you'll need to connect to the database everywhere in your application (use search and replace) using this wrapper function, and as a result, you'll always have the rowcount set.

like image 45
KM. Avatar answered Sep 28 '22 23:09

KM.