(Transfer) Get the previous N records in the database

xiaoxiao2021-03-06  41

If we want to write a stored procedure, get the top N records in the table, where n is the parameter, as an example of the Ordery table in the Northwind library, there are two ways:

The first kind is spelling SQL

Create Procedure Gettopnorders (@rowstoreTurn Int) asclare @SQL VARCHAR (8000)

Set @ SQL = 'SELECT TOP' CAST (@rowstoreturn as varchar) '* from Orders'

EXEC (@SQL)

The second is to borrow the RowCount property

Create Procedure Gettopnorders (@RowstoreTurn Int) Asset Rowcount @rowstoreTurnselection * from Orders Order by OrderID

For this example, no matter which effect is the same, but the second kind should be more flexible, just know more people!

Gettopnorders 20

转载请注明原文地址:https://www.9cbs.com/read-79174.html

New Post(0)