当前位置: 代码迷 >> ASP.NET >> 一次向一个表中插入多条数据,SQL应该如何样写效率最好
  详细解决方案

一次向一个表中插入多条数据,SQL应该如何样写效率最好

热度:4143   发布时间:2013-02-25 00:00:00.0
一次向一个表中插入多条数据,SQL应该怎么样写效率最好?
一次向一个表中插入多条数据,SQL应该怎么样写效率最好?

我现在是这样的 效率好吗?还有什么更好的吗?

INSERT Biao SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx
UNION ALL SELECT xx,xx,xx,xx,xx

------解决方案--------------------------------------------------------
不太懂,你是什么意思。
------解决方案--------------------------------------------------------
用dataadapter,一次update~~
------解决方案--------------------------------------------------------
你那些 SELECT xx,xx,xx,xx,xx 是从几个表里查出来?如果是一个表的话直接用一个SELECT把所有的查出来,一次性插进去,就不需要用UNION ALL了,如果从不同的表的话可以那样做.
------解决方案--------------------------------------------------------
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.
------解决方案--------------------------------------------------------
C# code
Sqlconnection connection=new SqlConnection();string sql="INSERT Biao SELECT xx,xx,xx,xx,xx UNION ALL SELECT xx,xx,xx,xx,xx UNION ALL SELECT xx,xx,xx,xx,xx UNION ALL SELECT xx,xx,xx,xx,xx";SqlComand command=new SqlCommand(sql,connection);connection.Open();int count=command.ExecuteNonQuery();connection.Close();
------解决方案--------------------------------------------------------
探讨
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.

------解决方案--------------------------------------------------------
都差不对!!
------解决方案--------------------------------------------------------
存储过程效率高
------解决方案--------------------------------------------------------
都差不多
------解决方案--------------------------------------------------------
批量的搞法,可以这样,一次insert一条,用 insert into table(...) values(...) 的方法,可以积攒到 1000 条左右的时候再去提交,这样比插入一条提交一条的速度要快一些,但不是很明显。
------解决方案--------------------------------------------------------
不是太影响速度,随意吧
------解决方案--------------------------------------------------------
探讨
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.

------解决方案--------------------------------------------------------
探讨
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.

------解决方案--------------------------------------------------------
SQL code
declare @x intset @x=0;while (@x<100000)begininsert table  ---这里是SQL语句set @x=@x+1end
------解决方案--------------------------------------------------------
insert into talbe1
select * from table2
------解决方案--------------------------------------------------------
[color=#FFFFFF][/color]
探讨
分开写快

insert into 表 select ..........
;insert into 表 select ........


但可放在一起执行.

------解决方案--------------------------------------------------------
1、建立临时表,写组合数据进临时表, 取出插往目标物理表,清空临时表!
就这样做!

2、不清楚你的意思:
如果只是测试
请这样
declare @x int
set @x=0;

while (@x<100000)
begin
insert table ---这里是SQL语句
set @x=@x+1
end

------解决方案--------------------------------------------------------
用dataadapter比较好,但是如果考虑到并发的问题就不好了
可以写一个insert循环执行
  相关解决方案