I have a table valued function called split
in my database and I want to use it in a stored procedure.
When I say entity I mean department in company, as you know companies has department, section and other divisions under each other. When user creates entity it should have parent which is the company or another entity and maybe have a child which is another entity e.g IT>Development>software... etc.
Each one has id. I am storing these ids in one column like this 1,2,1. I need to split them when am showing to users.
Here is my function:
ALTER FUNCTION [dbo].[Split](@String varchar(50), @Delimiter char(1))
returns @temptable TABLE (items varchar(50))
as
begin
declare @idx int
declare @slice varchar(50)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
Here is my stored procedure:
alter procedure [dbo].[Emp_CompanyHirarchy]
@FK_CompanyId varchar(50),
@FK_EntityId varchar(50)
AS
SELECT
Employee.EmployeeId, Employee.EmployeeNo,
Employee.EmployeeName, Employee.EmployeeArabicName,
OrgEntity.EntityName, OrgCompany.CompanyName,
Employee.DOB, Employee.Email
FROM
Employee
INNER JOIN
OrgEntity ON Employee.FK_EntityId = OrgEntity.EntityId
INNER JOIN
OrgCompany ON OrgEntity.FK_CompanyId = OrgCompany.CompanyId
WHERE
(Employee.FK_EntityId IN (dbo.Split(EntityHierarchy)))