Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.
How to insert multiple records by using single insert statement in stored procedure?


Thanks in advance...
Posted
Comments
AshishChaudha 9-Jul-12 4:47am    
do you want to insert records from another source file???
Ganesh_mca 9-Jul-12 4:48am    
no only in sp?
Ganesh_mca 9-Jul-12 6:19am    
Thanks one and all...

You can use Bulk Insert Task

The Bulk Insert task copies data from text files into a SQL Server table or view.
If you have data in text files, and the data requires no transformation, the Bulk Insert task is the quickest way to load data from text files into SQL Server.

You can use a format file together with the Bulk Insert task to define the fields and data types, or you can set the bulk insert options in the task.

The Bulk Insert task supports both XML and nonXML format files.

For more information about this task, see Bulk Insert Task[^].

The Bulk Insert task uses a File connection manager to connect to the source file and an OLE DB connection manager to connect to the Database Engine.

For more information, see Flat File Connection Manager[^] and OLE DB Connection Manager[^].


Look at some examples how you can insert multiple records:
SQL
-- Create table
CREATE TABLE dbo.MyTable 
            (my_value VARCHAR(20) NOT NULL,
             my_rec   INT         NOT NULL);
	          
-- Insert multipe records into table - prior to sql server 2008 (option 1)
INSERT INTO dbo.MyTable 
           (my_value, 
            my_rec)
VALUES     ('One',
            1);	          
INSERT INTO dbo.MyTable 
           (my_value, 
            my_rec)
VALUES     ('Two',
            2);
INSERT INTO dbo.MyTable 
           (my_value, 
            my_rec)
VALUES     ('Three',
            3);	    

-- Insert multipe records into table - prior to sql server 2008 (option 2)
INSERT INTO dbo.MyTable 
           (my_value, 
            my_rec)
SELECT      'Four', 4
UNION ALL
SELECT      'Five', 5
UNION ALL
SELECT      'Six', 6;

-- Insert multiple records into table - sql server 2008
INSERT INTO dbo.MyTable 
           (my_value, 
            my_rec)
VALUES     ('Seven', 7),
           ('Eight', 8),
           ('Nine', 9)

-- Select records
SELECT * FROM dbo.MyTable;


Also refer:
Bulk Inserts[^]
Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function[^]
 
Share this answer
 
Ganesh,

You can use bulk insert command of SQL server. What you need to do is to create a excel sheet with coloums mapped with (with same name) your sql server table.

this MSDN guide can help you further.

Bulk Import to SQL SERVER
 
Share this answer
 
Hi Ganesh....

U can use the below query to insert record using insert statement.

SQL
insert into sample1 values ('arul',23), ('arul',30)


Note: U can use ,(comma) between next values.

It may be help you
 
Share this answer
 
Comments
Ganesh_mca 9-Jul-12 4:41am    
But when i insert `one thousand records?how cn i write?
Arul R Ece 9-Jul-12 4:53am    
You can use "BULK INSERT "
Bulk Insert:

SQL
BULK
INSERT user_list
FROM 'E:\sharedE\userlist.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)



Mentioned the film name , contain data in server.
 
Share this answer
 
Comments
Ganesh_mca 9-Jul-12 4:57am    
i didnt get exactly how can i used?
Arul R Ece 9-Jul-12 6:06am    
U put the value in notepad n save in server.
Then mentioned server path in above mentioned place
You can also use the below syntax


SQL
INSERT INTO TableName (Column1, Column2)
SELECT Value_11, Value_12 UNION ALL
SELECT Value_21, Value_22 UNION ALL
.
.
.
SELECT Value_n1, Value_n2
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900