0

Is there any way to bind an array, which are generated in c++, to a parameter in SQL-Statement. Something like this:

insert into Table1(COLUMN1, COLUMN2, COLUMN3, datum) values (:1,:2,:3,:4)

in this example :1, :2, :3, :4 are array parameter.

I know Table Variable is a very nice idea, but I would like to use another approache

6
  • Other classical methods are passing an XML as a parameter, or composing a string with the values, with a separator, like 1,2,3,4,5 and then splitting it SQL side Commented Aug 14, 2015 at 9:20
  • But I see it difficult that you are able to insert into a table 4 arrays... there isn't a column of type array in SQL Server Commented Aug 14, 2015 at 9:21
  • stackoverflow.com/questions/8898203/… Commented Aug 14, 2015 at 9:27
  • Actually I dont need to inser an array in field in SQL server. I would like to add the content of each Array in a table. Commented Aug 14, 2015 at 9:28
  • @SantoshDhanawade thanks, but if the length of my array is 100, then I should run the INSERT statement 100 times. Somehow I would like to BULK INSERT of arrays Commented Aug 14, 2015 at 9:29

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.