sql 一次插入多条记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 一次插入多条记录相关的知识,希望对你有一定的参考价值。
一个按钮需要响应8条sql插入语句,如果每次执行一条,需要设8个变量,执行8次,效率太低,怎么做比较好
"insert into takecard (name,time,mianzhi,shuliang) values ('"+TextBox9.Text+"',getdate(),'20','"+TextBox1.Text+"')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'30','" + TextBox2.Text + "')
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'50','" + TextBox3.Text + "')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'100','" + TextBox4.Text + "')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'200','" + TextBox5.Text + "')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'500','" + TextBox6.Text + "')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'1000','" + TextBox7.Text + "')";
"insert into takecard (name,time,mianzhi,shuliang) values ('" + TextBox9.Text + "',getdate(),'10000','" + TextBox8.Text + "')";
需要用存储过程的话,应该怎么写呢? 我用的是 sql server 2005数据库
--添加一条记录
insert
into
tablename(col1,col2,col3)
values
(1,2,3)
--添加多条记录
insert
into
tablename(col1,col2,col3)
select
3,4,5
union
all
select
6,7,8
--从另外的一张表中读取多条数据添加到新表中
insert
into
tablename(col1,col2,col3)
select
a,b,c
from
tablea
--从其他的多张表中读取数据添加到新表中
insert
into
tablename(col1,col2,col3)
select
a,b,c
from
tablea
where
a=1
union
all
select
a,b,c
from
tableb
where
a=2
上边代码中的into都可以省略!
上边代码中的union
all如果换成union,则相同记录只插入一次,不会重复插入。
另外一种方法是sql
server2008特有的,所以,如果你不是sql
server2008,就不能使用这种方法了。
insert
into
mytable(id,name)values(7,'003'),(8,'004'),(9,'005')
create
table
[test]
(
[num_id]
int
primary
key
)
go
declare
@temp
int
set
@temp=1;
while
@temp<=1000000
begin
insert
into
[test]([num_id])
values(@temp)
set
@temp=@temp+1;
end
go
----------------------------------------------------------
--试试下面的方法
--2005
declare
@n
as
bigint;
set
@n
=
1000000;
with
base
as
(
select
1
as
n
union
all
select
n
+
1
from
base
where
n
<
ceiling(sqrt(@n))
),
expand
as
(
select
1
as
c
from
base
as
b1,
base
as
b2
),
nums
as
(
select
row_number()
over(order
by
c)
as
n
from
expand
)
select
n
from
nums
where
n
<=
@n
option(maxrecursion
0);
--2
create
function
dbo.fn_nums(@n
as
bigint)
returns
table
as
return
with
l0
as(select
1
as
c
union
all
select
1),
l1
as(select
1
as
c
from
l0
as
a,
l0
as
b),
l2
as(select
1
as
c
from
l1
as
a,
l1
as
b),
l3
as(select
1
as
c
from
l2
as
a,
l2
as
b),
l4
as(select
1
as
c
from
l3
as
a,
l3
as
b),
l5
as(select
1
as
c
from
l4
as
a,
l4
as
b),
nums
as(select
row_number()
over(order
by
c)
as
n
from
l5)
select
n
from
nums
where
n
<=
@n;
go
--2000
这个会比前两个慢,但是前两个2000不能用
create
table
dbo.nums(n
int
not
null
primary
key);
declare
@max
as
int,
@rc
as
int;
set
@max
=
1000000;
set
@rc
=
1;
insert
into
nums
values(1);
while
@rc
*
2
<=
@max
begin
insert
into
dbo.nums
select
n
+
@rc
from
dbo.nums;
set
@rc
=
@rc
*
2;
end
insert
into
dbo.nums
select
n
+
@rc
from
dbo.nums
where
n
+
@rc
<=
@max;
-------------------------------------------------------------------------------------------------------- 参考技术A 有些数据库支持批量Sql,但是不建议这么做
实际上你在一个Open的数据库连接上连续执行8条Sql,效率不会多低,加上一个事务就好了,如:
SqlConnection con = new SqlConnection(constr);
con.Open();
// 使用事务,8条Sql要么都执行成功,要么都执行失败
SqlTransaction tran = con.BeginTransaction();
try
SqlCommand com = con.CreateCommand();
com.Transaction = tran;
com.CommandText = "insert....";
com.ExecuteNonQuery();
com.CommandText = "insert....";
com.ExecuteNonQuery();
com.CommandText = "insert....";
com.ExecuteNonQuery();
tran.Commit();
catch(Exception)
tran.Rollback();
finally
if (con.State != ConnectionState.Closed)
con.Close();
本回答被提问者采纳 参考技术B 你把8条语句放到同一个变量里面 每条之间用空格隔开 一次执行就ok啦
使用 Laravel 一次插入多条记录
【中文标题】使用 Laravel 一次插入多条记录【英文标题】:Insert Multiple Records At Once With Laravel 【发布时间】:2016-12-31 00:29:46 【问题描述】:我正在将数据逐行插入,但我在某处听说,如果要插入很多数据,则需要很长时间。那么有什么方法可以一次性全部插入呢?
public function add(Request $request)
if ($request->ajax())
$books = $request->books;
foreach ($books as $book)
if (!empty($book))
$add = new Book;
$add->name = $book;
$add->user_id = Auth::user()->id;
$add->save();
【问题讨论】:
Bulk Insertion in Laravel using eloquent ORM的可能重复 This older question may help最新评论回复链接到something helpful (我不是 laravel 开发人员)似乎一年多前的任何答案都不再相关?我想这取决于版本... 【参考方案1】:使用模型插入多条记录
正如其他人所指出的,使用查询生成器是一次插入多条记录的唯一方法。幸运的是,Laravel 和 Eloquent ORM 以许多有用的方式耦合在一起。这种耦合允许您使用模型来获取为该模型设置的查询生成器实例。
// use Auth;
// use Carbon;
// use App\Book;
public function add(Request $request)
if($request->ajax())
// Submitted books
$books = $request->books;
// Book records to be saved
$book_records = [];
// Add needed information to book records
foreach($books as $book)
if(! empty($book))
// Get the current time
$now = Carbon::now();
// Formulate record that will be saved
$book_records[] = [
'name' => $book,
'user_id' => Auth::user()->id,
'updated_at' => $now, // remove if not using timestamps
'created_at' => $now // remove if not using timestamps
];
// Insert book records
Book::insert($book_records);
【讨论】:
这太糟糕了,你发布了我的 @Qevo 的副本【参考方案2】:public function add(Request $request)
if($request->ajax())
$books=$request->books;
$data = array();
foreach($books as $book)
if(!empty($book))
$data[] =[
'name' => $book,
'user_id' => Auth::id(),
];
Book::insert($data);
<!--DB::table('books')->insert($data);-->
确保导入use Illuminate\Support\Facades\Auth;
【讨论】:
【参考方案3】:如果您需要 Eloquent model events - 没有其他方法可以插入多个模型。以其他方式-检查Anushan W答案
【讨论】:
【参考方案4】:您应该能够执行以下操作:
DB::table('users')->insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
]);
将所有要插入的值放入一个数组中,然后将其传递给插入函数。
来源:https://laravel.com/docs/5.1/queries#inserts
【讨论】:
以上是关于sql 一次插入多条记录的主要内容,如果未能解决你的问题,请参考以下文章