Passing array value to database stored procedure

Some of the applications may need to update the database procedure with bulk records at one go.
Instead of doing a loop calling the stored procedure, an array value can be passed to the stored procedure and update the database. I use this procedure to insert or update database values.



Code:
Create procedure usp_updaterecords (@ArrayInput varchar(8000)) as

BEGIN
    SET NOCOUNT ON

    DECLARE @Row varchar(10), @Position int

    SET @ArrayInput = LTRIM(RTRIM(@ArrayInput))+ ','
    SET @Position = CHARINDEX(',', @ArrayInput, 1)

    IF REPLACE(@ArrayInput, ',', '') <> ''
    BEGIN
      WHILE @Position > 0
      BEGIN
         SET @Row = LTRIM(RTRIM(LEFT(@ArrayInput, @Position - 1)))
         IF @Row <> ''
         BEGIN
               -- add your insert or update statement here
            
           END
         SET @ArrayInput = RIGHT(@ArrayInput, LEN(@ArrayInput) - @Position)
         SET @Position = CHARINDEX(',', @ArrayInput, 1)
      END
    END   
END

GO



Input value should be separated with �,� comma. This procedure splits the values and assigns it to @Row variable.
If you have a situation like this, I may be able to help you. Post your comments or suggestions here.

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net