在存储过程中使用一个插入语句插入多个记录 [英] Insert multiple records using one single insert statement in Stored procedure

查看:93
本文介绍了在存储过程中使用一个插入语句插入多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
如何在存储过程中使用单个插入语句插入多个记录?


在此先感谢...

Hi.
How to insert multiple records by using single insert statement in stored procedure?


Thanks in advance...

推荐答案

您可以使用批量插入任务

批量插入任务将数据从文本文件复制到SQL Server表或视图中.
如果文本文件中有数据,并且数据不需要转换,则批量插入任务是将文本文件中的数据加载到SQL Server中的最快方法.

您可以将格式文件与批量插入任务一起使用以定义字段和数据类型,也可以在任务中设置批量插入选项.

批量插入任务支持两个XML and nonXML格式文件.

有关此任务的更多信息,请参见批量插入任务 [ ^ ].

批量插入任务使用文件连接管理器连接到源文件,并使用OLE DB连接管理器连接到数据库引擎.

有关更多信息,请参见平面文件连接管理器 [ ^ ]和 OLE DB连接管理器 [
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:
-- 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;



另请参阅:
批量插入 [使用C#DataTable和SQL Server OpenXML函数批量插入数据 [ ^ ]



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


Ganesh,

您可以使用SQL Server的批量插入命令.您需要做的是创建一个Excel表,其中的coloums映射到(同名)您的sql server表.

此MSDN指南可以进一步为您提供帮助.

批量导入到SQL SERVER
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


Ganesh....

U可以使用以下查询通过插入语句插入记录.

Hi Ganesh....

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

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



注意:U可以在下一个值之间使用,(逗号).

可能对您有帮助



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

It may be help you


这篇关于在存储过程中使用一个插入语句插入多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆