I have a table that have three column in it

```
Agent_Listing_Company>-
ListingId, AgentId, CompanyId

```

I want to insert comma separated input in it. My input is like

```
ListingId = '5',  AgentId = '6,7,8', CompanyId = '9,10,11'

```

I am using this query for insertion but its not working 

```
INSERT  INTO dbo.Agent_Listing_Company
                        (  ListingId ,
                          AgentId ,
                          CompanyId
                        )
                        SELECT  5, item ,
                                (select item from dbo.fn_split('9,10,11' , ','))
                        FROM    dbo.fn_split('6,7,8', ',')

```

--Here dbo.fn\_split('6,7,8', ',')split my CompanyIds, But how do i split two inputs. 
My saved value after insertion in the table should be

```
ListingId, AgentId, CompanyId
   5         6          9
   5         7          10
   5         8          11

```

My dbo.fn\_split('6,7,8', ',') function is...

```
USE [XRC]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_split]    Script Date: 6/3/2015 10:04:07 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER function [dbo].[fn_split](
@str varchar(max),
@delimiter char(1)
)
returns @returnTable table (idx int primary key identity, item varchar(8000))
as
begin
declare @pos int
select @str = @str + @delimiter
while len(@str) > 0 
begin
    select @pos = charindex(@delimiter,@str)
    if @pos = 1
        insert @returnTable (item)
            values (null)
    else
        insert @returnTable (item)
            values (substring(@str, 1, @pos-1))
    select @str = substring(@str, @pos+1, len(@str)-@pos)       
end
return
end

```