Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server default for Select Top 1000 Rows

Is there a way to get this to default to the following: order by Id descending, or ordering by row creation date descending?

like image 399
Brad Thomas Avatar asked Jun 28 '13 19:06

Brad Thomas


People also ask

How do I limit 1000 rows in SQL?

You can easily change this limit by going to MySQL Workbench >> Edit >> Preferences >> SQL Queries tab. Over here you will option to Limit Rows. You can set this to very high value or uncheck the option. When you uncheck that option, it will retrieve all the rows from a query (equivalent to no limits).

How do I increase Edit Top 200 rows in SQL Server?

Using SQL Server Management Studio In Object Explorer, expand the database that contains the view and then expand Views. Right-click the view and select Edit Top 200 Rows. You may need to modify the SELECT statement in the SQL pane to return the rows to be modified.

How do I select 100 rows in SQL Server?

All replies. select <column list you want> from <your table name> order by ProductName offset 100 rows fetch next 100 rows only; That will skip the first 100 rows (in order by ProductName) and return the next 100 rows.


1 Answers

No. The only property that can be modified is the amount of rows under Tools -> Options -> SQL Server Object Explorer -> Value for Select Top < n > Rows command. Please note you can also modify the Edit Top < n > Rows and Select Top < n > Audit records.

You could write a separate stored procedure, add it to your master database, and add a shortcut under Tools -> Options -> Keyboard -> Query Shortcuts. Then in Management Studio, you could highlight the name of the schema and table and run the shortcut. The stored procedure called could then do this for you. Below is a sample stored procedure that would accomplish this goal but orders by the first column instead of a specific column name. You can replace the 1 with a specific column name, but this is likely more flexible for your purposes.

CREATE PROCEDURE dbo.usp_Test_OrderByFirstColumnDesc
    @TableName VARCHAR(MAX)
AS

SET NOCOUNT ON;

DECLARE @SqlCommand NVARCHAR(MAX) = N'SELECT TOP 1000 *
FROM ' + @TableName + ' (NOLOCK)
ORDER BY 1 DESC';

PRINT @SqlCommand;

EXEC dbo.sp_ExecuteSQL @SqlCommand;
like image 91
Registered User Avatar answered Nov 07 '22 18:11

Registered User