SQL Server – use a parameter to select the top X of the result set [duplicate]

In SqlServer 2005 and up, do this: CREATE PROCEDURE GetResults ( @ResultCount int ) AS SELECT top(@ResultCount) FROM table where x = y For earlier versions, use: CREATE PROCEDURE GetResults ( @ResultCount int ) AS SET ROWCOUNT @ResultCount SELECT * FROM table where x = y https://web.archive.org/web/20210417081325/http://www.4guysfromrolla.com/webtech/070605-1.shtml for more information.

How to Replace Multiple Characters in SQL?

One useful trick in SQL is the ability use @var = function(…) to assign a value. If you have multiple records in your record set, your var is assigned multiple times with side-effects: declare @badStrings table (item varchar(50)) INSERT INTO @badStrings(item) SELECT ‘>’ UNION ALL SELECT ‘<‘ UNION ALL SELECT ‘(‘ UNION ALL SELECT ‘)’ … Read more

Use a Query to access column description in SQL

If by ‘description’ you mean ‘Description’ displayed in SQL Management Studio in design mode, here it is: select st.name [Table], sc.name [Column], sep.value [Description] from sys.tables st inner join sys.columns sc on st.object_id = sc.object_id left join sys.extended_properties sep on st.object_id = sep.major_id and sc.column_id = sep.minor_id and sep.name=”MS_Description” where st.name = @TableName and sc.name … Read more

max date record in LINQ

Starting from .NET 6 MaxBy LINQ method is available. var result = items.MaxBy(i => i.Date); Prior to .NET 6: O(n): var result = items.Aggregate((x, y) => x.Date > y.Date ? x : y); O(n log n): var result = items.OrderByDescending(i => i.Date).First(); O(n) – but iterates over the sequence twice: var max = items.Max(i => … Read more