简体   繁体   中英

How to Insert N Records in a Single Stored Procedure Call

How would I accomplish being able to insert an unknown number of records into the database using a single stored procedure call?

Say I have this table definition:

CREATE TABLE PHONE(
    PhoneID int identity(1,1),
    PhoneNumber varchar(20),
    PersonID int
)

And I have the following insert stored procedure:

CREATE PROCEDURE dbo.usp_Phone_Insert
    @PhoneNumber varchar(20),
    @PersonID int
AS

INSERT 
    PHONE 
VALUES
    (@PhoneNumber, @PersonID)

How would I be able to transform this to insert any number of records in a single call to this stored procedure?

I have some blog posts with code samples that address this topic.

Performance Comparison of Singleton, XML, and TVP Inserts http://sqlfool.com/2008/11/performance-comparison-of-singleton-xml-and-tvp-inserts/

Bulk Inserts with XML http://sqlfool.com/2008/11/bulk-inserts-with-xml/

One-to-Many Inserts with Table-Valued Parameters http://sqlfool.com/2008/11/one-to-many-inserts-with-table-valued-parameters/

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM