SQL User-Defined Functions: Fetching TOP n records in a user-defined function?

For MS SQL 2000, you can use: CREATE FUNCTION Test (@top integer) RETURNS TABLE AS SET ROWCOUNT @top RETURN SELECT * FROM SomeTable.

Support for this was added to SQL Server 2005, but it's not available in 2000. You'd have to use (shudder) dynamic sql or some other number trick instead.

...that is ridiculous. ;) – Jake Nov 12 '08 at 16:27.

CREATE FUNCTION Test (@top integer) RETURNS TABLE AS RETURN SELECT TOP (@top) * FROM SomeTable GO However without an ORDER BY clause it is not very meaningful (the order of the results is not guaranteed).

Oops a variable TOP is not available in SQL Server 2000. Nor is Dynamic SQL supported in any version.

Jake, try setting the rowcount to your function parameter and then doing your select. I have not tried this, YMMV. From: msdn.microsoft.com/en-us/library/aa25918...).aspx Syntax SET ROWCOUNT { number | @number_var } Arguments number | @number_var Is the number (an integer) of rows to be processed before stopping the given query.

You can't used set rowcount in a user-defined function – Terrapin Dec 8 '08 at 17:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions