How to insert multiple records in a table using a stored procedure

Hi

Many a times we have to insert multiple data on click of one button. Like inserting all the checked records in the favorite table of the member. It is not a good idea to insert the records by using as many insert queries.

At these points we should insert all the records with only one call to the database. We can use a stored procedure that takes a comma (or any separator you want to use) list of values and insert them in the database. This way the whole process will be very fast and efficient.

In this example we will insert data in the MemberProductfaveriote table. We will be insert the member ID and the MemberProductId(all the product selected as favorite from the list). We will pass the MemberID and the comma separated list of MemberProductId.

The stored procedure will insert each of the record in the database.

In the stored procedure we also check if the record is already inserted in the database. If the record in already present in the database we will not insert the record. We will only insert the record if the record in not present in the database. Here is the stored procedure

CREATE PROCEDURE dbo.insertMemberProductFavorite

      (

            @MemberId int,

            @MemberProductId varchar(100)

      )

AS

      DECLARE @count int

      DECLARE @str VARCHAR(8000)

      DECLARE @spot SMALLINT

 

    WHILE @MemberProductId <> ''

    BEGIN

        SET @spot = CHARINDEX(',', @MemberProductId)

        IF @spot>0

            BEGIN

                SET @str = CAST(LEFT(@MemberProductId, @spot-1) AS INT)

                SET @MemberProductId = RIGHT(@MemberProductId, LEN(@MemberProductId)-@spot)

            END

        ELSE

            BEGIN

                SET @str = CAST(@MemberProductId AS INT)

                SET @MemberProductId = ''

            END

          

     SELECT @count=count(1) FROM MemberProductFavorite

     WHERE MemberProductId=@str

     AND MemberId=@MemberId

    

     IF @count =0

     BEGIN

      INSERT INTO MemberProductFavorite

                                    (MemberId,

                                    MemberProductId)

                    VALUES

                              (      @MemberId,

                                    @str)

     END

    END

      RETURN

GO

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 11/9/2006 7:23:06 AM

Vikram,

Based on your post on Kolkatanet, some comments were posted in that thread, I invite you to join that thread.

http://groups.msn.com/kolkatanet/general.msnw?action=get_message&mview=0&ID_Message=2382&LastModified=4675597095398210024

Posted on 11/9/2006 5:34:24 PM

Hi Richard,

Sorry that I could not join the thread fast enough. I was busy with some work. I will surely join the thread.

Vikram

Posted on 11/10/2006 7:20:27 AM

Didn't you consider to use OPENXML function?

Posted on 12/29/2006 6:51:49 AM

It is not correct. Because It is not correctly explained

Posted on 12/29/2006 4:34:54 PM

Hi naninani

Can u please explain what is wrong here. I have used this and it works perfectly

Posted on 12/29/2006 4:37:00 PM

Hi Vadimas

Yes I did considered the open XML function. You can also check the discussion on the topic in the kolkatanet group
http://groups.msn.com/kolkatanet/general.msnw?action=get_message&mview=0&ID_Message=2382&LastModified=4675597095398210024

Posted on 3/31/2008 4:53:57 AM

The loop is broken out of before the last record is traversed.

Posted on 1/4/2009 11:51:19 PM

Hi Vikram,

I have a small issue which I am not able to resolve and all your help on this would be highly appreciated.

I have a form through which I am trying to insert a series into the database table. The series would start from 000000 and end on 999999, so we are talking about inserting 10 lakh records in one go which is not happening right now. I have gone through your article on inserting multiple rows in single go using stored procedure. Could you please provide me a more detailed solution for this problem and if i could get the stored procedure from you then it would be great. My table structure is

ClientId numeric
MobileId numeric
TACNo varchar (10)
SerialNumber varchar(10)

Thanks in advance for your help.

Amit

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia