Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Using a variable to select the top values

$
0
0

Hi Guys,

I'm working in a Table Valued Function that's referred to in a stored procedure.

In the Table Valued funciton, I want to tell it to get the top number of records based on the number of months declared in teh variable. My understanding is that this should be really simple, however, when I put the "Select Top (@NumOfMonths) etc" in the function, it starts complaining about the other sections of the function.

The current script is below (names changed to keep company stored procedures secure)

SQL
ALTERFUNCTION[dbo].[Table](@profileIdint,@currentDatedatetime,@testIduniqueidentifier,@numOfMonthsint,@filtertinyint)RETURNSTABLEASRETURN(selecttop(@numOfMonths)*fromdbo.Table(@profileId,@currentDate,@testId,@numOfMonths,@filter)orderbyDatedescstat.DataIdasDatad,dbo.GetStatus(Score,MatchingSource,Limit,@...

Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>