0

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)))
12
  • Is it a table or a scalar valued function? What is the function definition? What do you want to use it for? Commented Dec 18, 2012 at 8:29
  • forums.asp.net/t/380905.aspx/1 Commented Dec 18, 2012 at 8:29
  • it is a table valued function to split data in the same column Commented Dec 18, 2012 at 8:31
  • did you try using the function in sp and see what happens? Commented Dec 18, 2012 at 8:32
  • is the correct way to call it dbo.split (col name)???? Commented Dec 18, 2012 at 8:33

2 Answers 2

1

In General;

Select items From dbo.Split(YourColumnOrVariable,',')

In your case, (since you are using IN) WHERE clause should be as;

WHERE (Employee.FK_EntityId IN (Select Items FROM dbo.Split(EntityHierarchy)))
--Items is the column name of the returning table
Sign up to request clarification or add additional context in comments.

2 Comments

if i wrote select * from dbo.split(20,10) the result will be 20
@Abdulrahman_88: I think you need to find a good Split function. Also you should pass a string to the split function as dbo.Split('20,10')
1

Here is stackoverflow post.

  • Here is a sample of how to call a function within a stored procedure.

REference

DECLARE @Variable  NVARCHAR(MAX)
SELECT @Variable = [Values] FROM tbl_test WHERE ID = @colnumber
SELECT * FROM dbo.ufn_SplitText(@Variable,',')

You may use dbo.yourfunction() within the stored procedure.

Reference 2


UPDATE AS PER OP'S COMMENTS AND DATA VALUES

You need to split by Space since you data in EntityHierarchy = 20 10 30 26

try following line in your code:

WHERE (Employee.FK_EntityId IN (Select Items FROM dbo.Split(EntityHierarchy, ' ')))

5 Comments

if i wrote select * from dbo.split(20,10) the result will be 20
@Abdulrahman_88 what is your expected result? can you show us how does the value of your Entityhierarchy look like?
So you want to get all 4 values ? then do this: WHERE (Employee.FK_EntityId IN (Select Items FROM dbo.Split(EntityHierarchy, ' '))) means you do split by space
the col entityhierarchy include departments id for companies such IT includes develpment, QA,..etc. when user choose any child department i want to show user the parent department such as : IT> Development> Software as many as stored in db
@Abdulrahman_88 let's take step by step. You may update your question with 1. Table schema, and sample values 2. the expected answer :) So it makes much clearer for anyone here to help you with the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.