sqlserver存储过程批量插入数据
在系统中经常会遇到向数据库中批量插入数据情况,存储过程中没有数组,只有通过字符串分割循环插入,下面是一个本人研究的一个例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
create proc [dbo].[Proc_TestBatchMainDetailIns]@mainName nvarchar(50),@detailNameStr nvarchar(max),@detailAgeStr nvarchar(max),@detailRowCount int=1,@tmpFlag int=1,@newMainId int=0asbegininsert into TestProBatch_Main(MainName) values(@mainName) select @newMainId=@@IDENTITYset @detailRowCount=len(@detailNameStr)-len(replace(@detailNameStr,'|',''))+1set @detailNameStr=@detailNameStr+'|'set @detailAgeStr=@detailAgeStr+'|'while(@tmpFlag<=@detailRowCount)begininsert into TestProcBatch_Detail(MainId,DetailName,DetailAge) values(@newMainId,dbo.F_RtnStrBySplitIndex(@detailNameStr,@tmpFlag),dbo.F_RtnStrBySplitIndex(@detailAgeStr,@tmpFlag))set @tmpFlag=@tmpFlag+1endend |
这个例子是插入一条主单信息和对应的多条子信息,下面是两张表
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
--主表CREATE TABLE [dbo].[TestProBatch_Main]( [ID] [int] IDENTITY(1,1) NOT NULL primary key, [MainName] [nvarchar](50) NOT NULL, [CreateTime] [datetime] NOT NULL );--子表CREATE TABLE [dbo].[TestProcBatch_Detail]( [ID] [int] IDENTITY(1,1) NOT NULL primary key, [MainId] [int] NOT NULL, [DetailName] [nvarchar](50) NOT NULL, [DetailAge] [int] NOT NULL, [CreateTime] [datetime] NOT NULL ); |
dbo.F_RtnStrBySplitIndex是自定义的标量值函数,用于返回第几个分割符对应的字符串,如dbo.F_RtnStrBySplitIndex('jack|lilei|tom|nike',3) 则返回tom
下面是函数的创建
|
1
2
3
4
5
6
7
8
9
10
11
12
|
create function [dbo].[F_RtnStrBySplitIndex](@procStr nvarchar(max),@splitStrIdx int)returns nvarchar(250)asbegindeclare @rtnStr nvarchar(250)declare @currentSplitIdx intdeclare @preSplitIdx intset @currentSplitIdx=dbo.F_RtnSomeCharIdxInStrByNo('|',@procStr,@splitStrIdx)set @preSplitIdx=dbo.F_RtnSomeCharIdxInStrByNo('|',@procStr,@splitStrIdx-1)set @rtnStr=SUBSTRING(@procStr,@preSplitIdx+1,@currentSplitIdx-@preSplitIdx-1)return @rtnStrend |
这个函数当中又用到了另一个函数dbo.F_RtnSomeCharIdxInStrByNo,用于返回某个字符在一个字符串的位置,下面是该函数的定义:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
ALTER function [dbo].[F_RtnSomeCharIdxInStrByNo](@findSplitStr varchar(250), @procStr varchar(8000), @n smallint) returns intasbegin if @n < 1 return (0) declare @start smallint, @count smallint, @index smallint, @len smallint set @index = charindex(@findSplitStr, @procStr) if @index = 0 return (0) else select @count = 1, @len = len(@findSplitStr) while @index > 0 and @count < @n begin set @start = @index + @len select @index = charindex(@findSplitStr, @procStr, @start), @count = @count + 1 end if @count < @n set @index = 0 return (@index)end |
调用存储过程:exec Proc_TestBatchMainDetailIns 'mainName1','jack|lilei|tom|nike','20|18|22|17'
下面是成功插入后查询到的结果:

sqlserver存储过程批量插入数据的更多相关文章
- PG数据库创建并执行存储过程批量插入数据
记录一下PG数据库创建并执行存储过程批量插入数据的SQL: create or replace function addId() returns boolean AS $BODY$ declare i ...
- mysql应用存储过程批量插入数据
--批量插入数据的sql语句 delimiter $$ DROP PROCEDURE IF EXISTS `test.sp_insert_batch` $$ CREATE DEFINER =`root ...
- Oracle 存储过程批量插入数据
oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...
- mysql利用存储过程批量插入数据
最近需要测试一下mysql单表数据达到1000W条以上时增删改查的性能.由于没有现成的数据,因此自己构造,本文只是实例,以及简单的介绍. 首先当然是建表: [sql]view plaincopy CR ...
- 使用Oracle的存储过程批量插入数据
原文地址:http://www.cnblogs.com/liaoyu/p/oracle-procedure-batch-insert.html 作者:L君还在说之乎者也 最近在工作中,需要使用生成一些 ...
- mysql存储过程批量插入数据
DROP TABLE IF EXISTS TeachersInfo; CREATE TABLE TeachersInfo ( id INT NOT NULL AUTO_INCREMENT, teach ...
- postgres 使用存储过程批量插入数据
參考资料(pl/pgsql 官方文档): http://www.postgresql.org/docs/9.3/static/plpgsql.html create or replace functi ...
- C#批量插入数据到Sqlserver中的四种方式
我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...
- C#批量插入数据到Sqlserver中的三种方式
本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...
随机推荐
- 解决Ruby在IE11中报Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)的错误
转载地址:http://www.tuicool.com/articles/BRnqeu2 I was updating the browser WebDrivers for Seleno ...
- Linux内核内存管理子系统分析【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51298718 版权声明:本文为博主原创文章,未经博主允许不得转载. 还是那张熟悉 ...
- Intel+Ardruino 101 翻转时点灯
/* =============================================== Example sketch for CurieIMU library for Intel(R ...
- Master page and jquery
1.download latest jqury http://jquery.com/download/ 2.download latest jquery.ui, Theme select " ...
- 亚马逊EC2 ubuntu下安装mysql远程无法连接问题o
无法远程的原因有很多,我今天遇到的问题是通过navicat无法远程连接我在EC2上创建的实例. 1.通过命令" netstat -an|grep 3306 "检查一下3306端口对 ...
- Linux下Ruby开发配置
以CentOS为例 安装ruby:yum install ruby 安装ruby devel:yum install ruby-devel,这个装上以后,就可以使用gem安装第三方模块了 安装gem, ...
- C# 使用lock关键字lock不同的对象
c# lock关键字的本质 是调用Monitor.Enter(object obj)并且在finally的时候调用Monitor.Exit(obj) 在obj是不同数据类型的时候会出现不同的情况 1. ...
- jodaTime 的使用说明
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifact ...
- PHP文本框的值随下拉框改变
初学PHP,下面是实现文本框内容随下拉框变化的代码实现: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- HTML基础(1)
1.前端开发语言介绍 HTML(Hypertext Markup Language)—— 结构—— 超文本标记语言 CSS(Cascading Style Sheets)—— 样式—— 层叠样式表 J ...