Using the suggestion given by Xantos, pass your array into a stored procedure in the form of a delimited string. You can then use the delimited string in a table-valued function.
ALTER FUNCTION [dbo].[String_To_Int_Table]
(
@list NVARCHAR(1024)
, @delimiter NCHAR(1) = ',' --Defaults to CSV
)
RETURNS
@tableList TABLE(
value INT
)
AS
BEGIN
DECLARE @value NVARCHAR(11)
DECLARE @position INT
SET @list = LTRIM(RTRIM(@list))+ ','
SET @position = CHARINDEX(@delimiter, @list, 1)
IF REPLACE(@list, @delimiter, '') <> ''
BEGIN
WHILE @position > 0
BEGIN
SET @value = LTRIM(RTRIM(LEFT(@list, @position - 1)));
INSERT INTO @tableList (value)
VALUES (cast(@value as int));
SET @list = RIGHT(@list, LEN(@list) - @position);
SET @position = CHARINDEX(@delimiter, @list, 1);
END
END
RETURN
END
You can then use the table function to fill other tables...
-- check to see if contacts were included...
if len(ltrim(rtrim(@Contacts)))> 0
begin
--create a temp table to hold the list of ids
CREATE TABLE #TmpContacts (ID INT);
-- use the table valued function to parse the ids into a table.
INSERT INTO #TmpContacts(ID)
SELECT Value FROM dbo.String_to_int_table(@Contacts, ',');
-- Select the @InterfaceID and the parsed ContactTypeIDs
-- for a bulk insert into the relational table...
INSERT INTO [InterfaceContacts]
([InterfaceID]
,[ContactID]
,[Created]
,[CreatedBy])
Select @InterfaceID, T.ID, CURRENT_TIMESTAMP, sUser_sName()
FROM #TmpContacts T;
drop table #TmpContacts;
end
1,2,3,4,5and then splitting it SQL side