Lumpi
Lumpi

Reputation: 2821

Defining several input variables in sql (server 2008) inline function

I want to modify my inline function so that it can handle two variables as input. With just one it worked just fine.

FUNCTION [dbo].[TBL_UDF_HowOften]
(   
-- Add the parameters for the function here
@membername as varchar(15),
@tablename as varchar(15)
)
RETURNS @ergebnis TABLE
(
participated            float,
percent                 float,
WonWen1                 float,
WonWen2                 float,  
WonWen3                 float
)

AS
BEGIN

    DECLARE @wintimes       float
    DECLARE a lot of other stuff...

    SELECT @wintimes = COUNT(DidWin)
    FROM @tablename
    WHERE DidWin = 1 AND membername = @membername

... and so on

Well, @membername is recognized but @tablename is marked with "Must declare the table variable "@tablename"."

Upvotes: 1

Views: 967

Answers (1)

Ray
Ray

Reputation: 21905

You can't use a scalar variable as the table name in a 'from' clause. You would need to use dynamic sql, which I do not think can be done inside a function.

Upvotes: 4

Related Questions