Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Calling a function from a database field

Author  Topic 

thenearfuture
Starting Member

35 Posts

Posted - 2008-12-29 : 17:46:04
Is there a way to call a function by referring to a database field?

Example:

There are 2 ProductIDs. Each ProductId has been assigned a different function. In my code, I have a ProductId and I have to look up and call the function that is assigned to that ProductId.


ProductId	FunctionName	   
1 dbo.SampleFunction1
2 dbo.SampleFunction2



I know that it's possible to do this with stored procedures, but I'd like to know if it can be done with functions.

Here is some sample code to show what I am trying to accomplish. Please note that the sample function code is just a quick mockup and doesn't really make sense :).

--- Set up the sample functions

CREATE FUNCTION SampleFunction1
(@ProductId INT) returns
@Info TABLE
(ProductId INT,
TestColumn VARCHAR (30))
AS
BEGIN
INSERT
INTO @Info
SELECT 1, '1 - It works!' RETURN
END


CREATE FUNCTION SampleFunction2
(@ProductId INT) returns
@Info TABLE
(ProductId INT,
TestColumn VARCHAR (30))
AS
BEGIN
INSERT
INTO @Info
SELECT 2, '2 - It works!' RETURN
END

--- Set up the Lookup table

DECLARE @Lookup TABLE
(ProductId INT,
FunctionName VARCHAR(30))
INSERT
INTO @Lookup
SELECT 1, 'dbo.SampleFunction1' UNION ALL
SELECT 2, 'dbo.SampleFunction2'

--- Find the function that goes with the given ProductId (ProdId)

DECLARE @ProdId INT
SET @ProdId = 1

DECLARE @FunctionName VARCHAR(30)
SET @FunctionName =
(SELECT lkp.FunctionName
FROM @lookup lkp
WHERE lkp.ProductId = @ProdId)

-- Here, I would like to call the function using @FunctionName, but can't figure out if it's even possible to do that.
-- If this were a stored procedure, I could just use EXEC @ProcedureName.

-----This is the sort of thing I'm looking for, where this type of code would call the function:
-----SELECT *
-----FROM (@FunctionName)

-- Drop the sample functions

DROP FUNCTION SampleFunction1
DROP FUNCTION SampleFunction2


Thank you for your help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-29 : 17:47:29
SELECT * FROM dbo.Function1(...)

You shouldn't do it dynamically, but you can do it via dynamic SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 00:57:06
if you want to run the function by retrieving name using variable you need dynamic sql, something like

EXEC('SELECT * FROM ' + @FunctionName + '(parameters here)')


didnt understand why you want to do it this way though
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-30 : 01:04:52
It is bad for performance and security to do it that way.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -