Is there any way to create a custom function that returns a table using a SQL statement that is created dynamically?
So far I tried things like
ALTER function [dbo].[Test2] (@Mandant smallint)
RETURNS
@Ergebnis TABLE ([Kundennummer] int,[Suchbegriff] nvarchar(20))
AS
BEGIN
DECLARE @curTable cursor
DECLARE @str nvarchar(1024)
DECLARE @Kundennummer int;
DECLARE @Suchbegriff nvarchar(20);
SET @str = 'set @curTable = CURSOR FOR SELECT KdNr, Match FROM ADR_030 where KdNr <> 0; OPEN @curTable'
EXEC sp_executesql @str, N'@curTable cursor OUTPUT', @curTable OUTPUT
fetch next from @curTable into @Kundennummer, @Suchbegriff;
WHILE @@FETCH_STATUS = 0
BEGIN
insert into @Ergebnis values (@Kundennummer, @Suchbegriff)
FETCH NEXT FROM @curTable into @Kundennummer, @Suchbegriff;
END;
close @curTable;
deallocate @curTable;
return
end
but just getting an error when executing the function.
Hope someone can help me here.
Thanks,
Chris