Search code examples
sql-server-2008stored-proceduresuser-defined-functions

Using a parameter in UDF from clause


I have a User defined function in SQL Server 2008. Here it is:

CREATE FUNCTION [dbo].[GetAddressByEntityNameAndId] 
(
-- Add the parameters for the function here
@entityName varchar (100),
@parentEntityId int
)
RETURNS varchar(300)
AS
BEGIN
-- Declare the return variable here
DECLARE @Address varchar(300)

if(@entityName = 'Staff')
BEGIN

    select @Address = ca.Address + ' ' + ca.City + ' ' + ca.State + ' ' + ca.ZipCode
    from @entityName cc 
    inner join  ContactAddress ca on ca.ParentEntityId = cc.Id 
    inner join EntityName en on en.Id = ca.EntityNameId and en.Name = @entityName 
    inner join GeneralLookup gl on ca.glAddressTypeId = gl.Id and gl.LookupItem = 'Primary' 
    where cc.Id = @parentEntityId
    
END 
-- Return the result of the function
RETURN @Address

END

But it does not get executed. Error message is:

Must declare the table variable "@entityName".

Update

Now I have another problem. This is my SP:

ALTER PROCEDURE [dbo].[spGetStaffsAndClients]

AS
BEGIN

declare @Address varchar (100)
declare @Apartment varchar (100)
declare @City varchar (100)
declare @State varchar (10)
declare @Zip varchar (10)
declare @County varchar (100)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

/* Get Client's Residence */

select dbo.GetClientFullName(s.FirstName, s.MiddleInit, s.LastName) StaffName, 
dbo.GetStaffTitlesById(s.Id) StaffTitle,
dbo.GetClientFullName(c.FirstName, c.MiddleInit, c.LastName) ClientName, 
dbo.GetAddressByEntityNameAndId('Client', c.Id) ClientAddress

from ClientStaff cs 
left outer join Staff s on cs.StaffId = s.Id 
left outer join Client c on cs.ClientId = c.Id

END

This is UDF:

ALTER FUNCTION [dbo].[GetAddressByEntityNameAndId] 
(
-- Add the parameters for the function here
@entityName varchar (100),
@parentEntityId int
)
RETURNS varchar(300)
AS
BEGIN
-- Declare the return variable here
DECLARE @Address varchar(300)

if(@entityName = 'Client')
BEGIN

    select @Address = ca.Address + ' ' + ca.City + ' ' + ca.State + ' ' + ca.ZipCode
    from Client cc 
    inner join  ContactAddress ca on ca.ParentEntityId = cc.Id 
    inner join EntityName en on en.Id = ca.EntityNameId and en.Name = cc.Id 
    inner join GeneralLookup gl on ca.glAddressTypeId = gl.Id and gl.LookupItem = 'Primary' 
    where cc.Id = @parentEntityId
    
END 
-- Return the result of the function
RETURN @Address

END

I am executing the SP and getting error:

Conversion failed when converting the varchar value 'Client' to data type int.

How can I resolve this?


Solution

  • You can't use @entityName in the from clause instead of a table name. Since you only test on Staff you can use from Staff cc instead. The only way I know of to have a variable table name in the from clause is to use dynamic SQL. And I don't think you can use dynamic SQL in functions.

    Read more about dynamic queries here http://www.sommarskog.se/dynamic_sql.html.