У меня есть пользовательская функция в SQL Server 2008. Вот она:
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
Но она не выполняется.Сообщение об ошибке:
Must declare the table variable "@entityName".
Любая помощь будет оценена.
ОБНОВЛЕНИЕ:
Хорошо, теперь у меня есть другая проблема.Это мой 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
Это 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
Я выполняю SP и получаю ошибку: Conversion failed when converting the varchar value 'Client' to data type int.
Я не могу решить проблему.Любая помощь?