你试试这样插入,可以吗?利用检索语句,搜索其它表中符合条件的一批记录,来插入到表中。 insert into table1 (column1,column2) select column1,column2 from table2你也可以这样做:insert into table1 select 1,'a' union all select 2,'b' union all select 3,'c' union all select 4,'d'
方法1是上面的 不過還可以這樣select 你需要的字段 into 目的表 from 源表 where 條件上面方法是把表的數據復制到新的表去
insert into table1 select 1,'a' union all select 2,'b' union all select 3,'c' union all select 4,'d' 这个我试了下,很容易就查询器用尽堆栈空间。我的机上2000条就不行了
insert into table1 (column1,column2)
select column1,column2 from table2你也可以这样做:insert into table1 select 1,'a'
union all select 2,'b'
union all select 3,'c'
union all select 4,'d'
不過還可以這樣select 你需要的字段 into 目的表 from 源表 where 條件上面方法是把表的數據復制到新的表去
union all select 2,'b'
union all select 3,'c'
union all select 4,'d'
这个我试了下,很容易就查询器用尽堆栈空间。我的机上2000条就不行了
http://community.csdn.net/Expert/topic/4133/4133929.xml?temp=.9734308
当中 unsigned有给楼主好建议,不妨参考一下这个问题也是实际数据写入database
1.unsigned的回复
回复人: unsigned(僵哥(当程序语言成为普及的第三语言之后……)) ( ) 信誉:100 2005-07-09 23:45:00 得分: 0
使用VB做批次提交。楼主提交数据的速度之所以慢,是因为程序多次与数据库做交互而产生了延迟,建议以500/1000条记录提交一次。
以下是偶在C++版滴回贴,可以参考一下。
http://community.csdn.net/Expert/topic/4098/4098581.xml?temp=.5737268
2.http://community.csdn.net/Expert/topic/4098/4098581.xml?temp=.5737268的unsigned的回复,
回复人: unsigned(僵哥(当程序语言成为普及的第三语言之后……)) ( ) 信誉:105 2005-6-23 16:51:31 得分: 0
偶去测试了一下,要提高速度就尽可能减少对数据库的访问,即以一次提交多笔的方式进行提交,当然,若是能够直接以Socket的方式传输,或许会更快,这或许也就是为什么ms的bcp之类的能高速度执行对ms sql的数据导入的原因之一吧。
举个例说吧:1.测试一:
==================================
DWORD StartTime=GetTickCount();for( int i=0;i<200000;i++)
{
if(ADOQuery->Active)
ADOQuery->Close();
ADOQuery->SQL->Text="Insert into tablename (field1,field2,field3[...,fieldn]) values(value1,value2,value3[...,valuen])";
ADOQuery->ExecSQL();
}
DWORD EndTime=GetTickCount();
ShowMessage((int)(EndTime-StartTime));
==================================2.测试二
==================================
const AnsiString SplitEmpty="",Split=";";DWORD StartTime=GetTickCount();int Count,iCacheSize=1000;TStringList *CacheList=new TStringList();
try
{ for(int i=0;i<200000;i++)
{
if(Count)
CacheList->Add(";Insert into tablename (field1,field2,field3[...,fieldn]) values(value1,value2,value3[...,valuen])");
else
CacheList->Add("Insert into tablename (field1,field2,field3[...,fieldn]) values(value1,value2,value3[...,valuen])"); Count++; if(Count==iCacheSize)
{
if(ADOQuery->Active)
ADOQuery->Close();
ADOQuery->SQL->Text=CacheList->Text;
CacheList->Clear();
Count=0;
ADOQuery->ExecSQL();
}
} if(Count>0)
{
if(ADOQuery->Active)
ADOQuery->Close();
ADOQuery->SQL->Text=CacheList->Text;
ADOQuery->ExecSQL();
}
}
__finally
{
CacheList->Free();
}DWORD EndTime=GetTickCount();ShowMessage((int)(EndTime-StartTime));
==================================分别执行这两段代码往数据库当中写入20万条记录,测试二当中大概的花费依机器配置情况,大概为2~5分钟,甚至更短,测试一当中时间的花费,将会远远超过测试二。