Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with TOP and User Functions

Status
Not open for further replies.

TechnoSpike

Programmer
Joined
Mar 23, 2005
Messages
3
Location
PT
Hi! I'm trying to create an User Defined function that gives me the top rows of a table. I must be able to define wich is the top number of rows to be returned. I assume I could do this:

CREATE FUNCTION ListTopRegions (@reg_id int, @num int)
RETURNS @table table ([TerritoryID] [nvarchar] (20), [TerritoryDescription] [nchar] (50))

AS
begin
if exists (select 1 from Region where RegionID = @reg_id)
begin
insert into @table select top @num TerritoryID, TerritoryDescription from Territories
where RegionID=@reg_id
end
return
end

but it keeps saying : Incorrect syntax near '@num'. Any idea what I can do to bypass this? I'm using the Northwing DB, and I intend to get the first 5 entries, for instance.
 
Try this...

Exec ('Select top ' + @num + 'cus_no, cus_name from arcusfil_sql')

...insert into @table
Exec ('Select top ' + @num + ' TerritoryID, TerritoryDescription from Territories Where RegionID= ' + @reg_id)
 
sorry, dropped some code in by mistake. Just use this...

...insert into @table
Exec ('Select top ' + @num + ' TerritoryID, TerritoryDescription from Territories Where RegionID= ' + @reg_id)

or this may work as well...

Exec ('insert into ' + @table + ' Select top ' + @num + ' TerritoryID, TerritoryDescription from Territories Where RegionID= ' + @reg_id)


 
Hum....sorry, doesn't work either....when I use this:

CREATE FUNCTION ListTopRegionsN (@reg_id int, @num int)
RETURNS @table table ([TerritoryID] [nvarchar] (20), [TerritoryDescription] [nchar] (50))
AS
begin
if exists (select 1 from Region where RegionID = @reg_id)
begin
insert into @table Exec (' Select top ' + @num + ' TerritoryID, TerritoryDescription from Territories Where RegionID= ' + @reg_id)
end
return
end

I get this error:
Error 197: EXECUTE cannot be used as a source when inserting into a table variable.

And when I do this:

CREATE FUNCTION ListTopRegionsN (@reg_id int, @num int)
RETURNS @table table ([TerritoryID] [nvarchar] (20), [TerritoryDescription] [nchar] (50))
AS
begin
if exists (select 1 from Region where RegionID = @reg_id)
begin
Exec ('insert into'+ @table+' Select top ' + @num + ' TerritoryID, TerritoryDescription from Territories Where RegionID= ' + @reg_id)
end
return
end

I get the following error:

Error 137: Must declare the variable @table.

Any idea on a possible solution?
 
hmmm... it works outside of a function. I know that...I'll try working on it some more.
 
It appears that you cannot use Exec with table variables inside udf's.


Here is an alternative...


1st create a stored procedure..

CREATE PROCEDURE usp_GetTerritories

@reg_id int,
@num int

AS

Exec('Select
Top ' + @num +
' TerritoryID, TerritoryDescription from Territories
where RegionID' + @reg_id)

-------------

2nd, call this code whereever you were before

CREATE TABLE #Territories(TerritoryID nvarchar(20),
TerritoryDescription nchar(50))

INSERT INTO #Territories
--usp_GetTerritories @reg_id, @num
EXEC usp_GetTerritories 1, 8

Select * From #Territories

Drop Table #Territories
 
forgot the = sign after "where RegionId"..
should be "where RegionId = ' + @reg_id)"..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top