Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:27:01+00:00 2026-05-23T10:27:01+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:27:02+00:00Added an answer on May 23, 2026 at 10:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 and would like to have a user defined function
I have a user defined function in a SQL Server 2005 database which returns
I have been writing a user-defined function in SQL Server: It looks like this
I have a user defined function in SQL called getBuisnessDays it takes @startdate and
So I have the following user defined type in my oracle database: CREATE OR
I have a requirement in SQL Server 2008 in development database Only DBA's (
I have a brain-teaser for all you SQL Server 2008 Reporting gurus out there
I have a table that contains a GEOMETRY data type. SQL Server 2008 ships
I have a user defined function in SQLite (an aggregator that calculates the product)
for SQL Server 2008 R2 I have a resultset that looks like this (note

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.