如何实现sql server存储过程 批量插入数据

xhmbldsh 2006-11-16 06:28:34
获取到待插入的数据后,如何在存储过程中批量插入?参数如何传递?
...全文
679 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eddie005 2006-11-16
  • 打赏
  • 举报
回复
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();

cm.Connection = cnn;
cm.CommandText = "select * from table1";
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cm);

DataTable dt = new DataTable();
da.Fill(dt);
da.FillSchema(dt,System.Data.SchemaType.Mapped);//


//下面插入多条数据
for(int i=0;i<10;i++)
{
DataRow newRow = dt.NewRow();
newRow[0] = 123;
newRow[1] = "abc";
//....
dt.Rows.Add(newRow);
}


//下面重新写回数据库
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da);//
cnn.Open();
da.Update(dt);
cnn.Close();

dt.AcceptChanges();

上面的批量更新的方法同样适用于存储过程,只不过,这时候就不要用SqlCommandBuilder了,而是分别为da创建使用存储过程的UpdateCommand、InsertCommand和DeleteCommand
daishengs 2006-11-16
  • 打赏
  • 举报
回复
如果是楼主是想那种,先通过程序取得数据保存在DataTable,然后再想通过存储过程,把这个DataTable中的数据批量添加,这是比较困难,一般的做法是把取得数据的SQL语句当参数传给存储过程,再批量复制添加的。
daishengs 2006-11-16
  • 打赏
  • 举报
回复
下面这个例子希望给楼主启发。

1,单条插入

INSERT INTO time_by_day


(time_id, the_date, the_year, month_of_year, quarter,day_of_month)

VALUES ('1101', '1999-10-1', '1999', '10', 'Q4','1')


2,单条插入:


INSERT INTO time_by_day

(time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

} AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC


3,循环插入:



DECLARE @MyCounter INT

SET @MyCounter = 0 /*设置变量*/

WHILE (@MyCounter < 2) /*设置循环次数*/

BEGIN

WAITFOR DELAY '000:00:10' /*延迟时间10秒*/

INSERT INTO time_by_day

(time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

} AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC


SET @MyCounter = @MyCounter + 1

END


4,插入以时间为变量的数据


DECLARE @MyCounter INT

declare @the_date datetime

SET @MyCounter = 0

SET @the_date = '1999-1-4'

WHILE (@MyCounter < 200000)

BEGIN

WAITFOR DELAY '000:00:10'

/*INSERT INTO time_by_day

(time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

} AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC

*/

insert into time_by_day (time_id,the_date)values('371',@the_date)

SET @the_date = @the_date + 1

SET @MyCounter = @MyCounter + 1

END
xhmbldsh 2006-11-16
  • 打赏
  • 举报
回复
请楼上的说仔细些,最好举例说明,多谢了
lizhizhe2000 2006-11-16
  • 打赏
  • 举报
回复
应该传给存储过程的是那些如何获取持插入数据的SQL语句

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧