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必然是比自增长要快的,因为你生 成 ...
随机推荐
- 【NFC】Android NFC API Reference中英文
0 Near Field Communication Near Field Communication (NFC) is a set of short-range wireless technol ...
- 分列:将excel单元格的内容拆分为两列
提要:处理excel数据时有时需要把单元格的内容拆分为两列,可能方便外部软件的链接,可能使数据显示更明晰等等,有人说直接剪切加粘贴不就可以了吗,但是有时数据过多,这样处理很不效率,网上搜索的方法说插入 ...
- Flowplayer-Setup
SOURCE URL: https://flowplayer.org/docs/setup.html 1. DOCTYPE At the top of your page declare the HT ...
- Android 反编译
Android 反编译 步骤:1.下载apktool 工具,这一步 主要是反编译 xml 文件. 步骤:2 把xx.smali 文件转为java 工具 (单个) 图形界面 下载dex2jar 和xj ...
- WPF上Arc Lisence的有关问题
WPF下Arc Lisence的问题代码如下: using System; using System.Collections.Generic; using System.Configuration; ...
- AJAX的问题
1.什么是AJAX AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. A ...
- Hibernate中两种删除用户的方式
第一种,是比较传统的,先根据主键列进行查询到用户,在进行删除用户 //删除数据 public void deleteStudent(String sno) { init() ; Student qu ...
- rest版的webservice
为了学习app做打算 今天就自学了下webservice,rest应该是其中一种 还有种就是soap,目前就先举个rest的demo吧 准备ws的jar和spring的jar,如何要连接数据的话就自行 ...
- js中attr 与find 获取属性值,
一.attr的用法 1. attr(属性名) //获取属性的值(取得第一个匹配元素的属性值.通过这个方法可以方便地从第一个匹配元素中获取一个属性的值.如果元素没有相应属性,则返回 und ...
- PHP 小方法之 计算两个时间戳之间相差的日时分秒
if(! function_exists ('timediff') ) { function timediff($begin_time,$end_time){ if($begin_time < ...