在SQL SERVER中执行批处理插入语句 [英] Execute batch insert statement in SQL SERVER
问题描述
在我的应用程序中,它将动态生成20,000行insert语句,并将其提交给sql server并运行。
当这些insert语句运行时,会发生错误
在资源轮询'default'中没有足够的系统内存来运行此查询。
有一种有效的方式来运行这些插入语句?
我的尝试:
有没有一种有效的方法来运行这些插入语句?
20,000行插入语句
像这样:
插入table1(....)值(...)
插入table2(....)值(...)
。
。
。
插入table20000(....)值(...)
我使用暂存策略,如果我有那种卷,我可以将数据批量复制到SQL Server中。这需要:
有一个包含与源数据列名相匹配的字段的临时表
登台表中的所有字段都应该是varchar
读取源数据
从数据库中获取登台表的空记录集
将源数据插入空DataTable
批量将DataTable复制到sql server
private int BulkCopy(DataTable dtTable,string sTableName,SqlConnection oConn)
{
try
{
SqlBulkCopy oBC = new SqlBulkCopy(oConn);
oBC.BulkCopyTimeout = 60000 ;
oBC.DestinationTableName = sTableName;
oBC.WriteToServer(dtTable);
return dtTable.Rows.Count;
}
catch(exception ex)
{
throw ex;
}
}
然后使用存储过程将临时数据转换为最终表。
In my application,it will generate 20,000 rows of insert statement dynamically, and submit it to sql server and run.
When these insert statements're running, an error occurs
There is insufficient system memory in resource poll 'default' to run this query.
is there an efficient way to run these insert statements?
What I have tried:
is there an efficient way to run these insert statements?
20,000 rows of insert statement
like this :
insert into table1(....) values(...)
insert into table2(....) values(...)
.
.
.
insert into table20000(....) values(...)
I use a staging strategy where I BULK COPY the data into SQL Server if I have that sort of volume. This entails:
Have a staging table with fields matching your source data column names
All fields in the staging table should be varchar
Read in the source data
Get an empty record set from the database for the staging table
insert the source data into the empty DataTable
Bulk Copy the DataTable into sql server
private int BulkCopy(DataTable dtTable, string sTableName, SqlConnection oConn) { try { SqlBulkCopy oBC = new SqlBulkCopy(oConn); oBC.BulkCopyTimeout = 60000; oBC.DestinationTableName = sTableName; oBC.WriteToServer(dtTable); return dtTable.Rows.Count; } catch (Exception ex) { throw ex; } }
Then use a stored procedure to transform your staging data to the final table.
这篇关于在SQL SERVER中执行批处理插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!