在系统中经常会遇到向数据库中批量插入数据情况,存储过程中没有数组,只有通过字符串分割循环插入,下面是一个本人研究的一个例子:

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=0
as
begin
insert into TestProBatch_Main(MainName) values(@mainName) select @newMainId=@@IDENTITY
set @detailRowCount=len(@detailNameStr)-len(replace(@detailNameStr,'|',''))+1
set @detailNameStr=@detailNameStr+'|'
set @detailAgeStr=@detailAgeStr+'|'
while(@tmpFlag<=@detailRowCount)
begin
insert into TestProcBatch_Detail(MainId,DetailName,DetailAge) values(@newMainId,dbo.F_RtnStrBySplitIndex(@detailNameStr,@tmpFlag),dbo.F_RtnStrBySplitIndex(@detailAgeStr,@tmpFlag))
set @tmpFlag=@tmpFlag+1
end
end

这个例子是插入一条主单信息和对应的多条子信息,下面是两张表

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)
as
begin
declare @rtnStr nvarchar(250)
declare @currentSplitIdx int
declare @preSplitIdx int
set @currentSplitIdx=dbo.F_RtnSomeCharIdxInStrByNo('|',@procStr,@splitStrIdx)
set @preSplitIdx=dbo.F_RtnSomeCharIdxInStrByNo('|',@procStr,@splitStrIdx-1)
set @rtnStr=SUBSTRING(@procStr,@preSplitIdx+1,@currentSplitIdx-@preSplitIdx-1)
return @rtnStr
end

这个函数当中又用到了另一个函数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 int
as
begin
    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存储过程批量插入数据的更多相关文章

  1. PG数据库创建并执行存储过程批量插入数据

    记录一下PG数据库创建并执行存储过程批量插入数据的SQL: create or replace function addId() returns boolean AS $BODY$ declare i ...

  2. mysql应用存储过程批量插入数据

    --批量插入数据的sql语句 delimiter $$ DROP PROCEDURE IF EXISTS `test.sp_insert_batch` $$ CREATE DEFINER =`root ...

  3. Oracle 存储过程批量插入数据

    oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...

  4. mysql利用存储过程批量插入数据

    最近需要测试一下mysql单表数据达到1000W条以上时增删改查的性能.由于没有现成的数据,因此自己构造,本文只是实例,以及简单的介绍. 首先当然是建表: [sql]view plaincopy CR ...

  5. 使用Oracle的存储过程批量插入数据

    原文地址:http://www.cnblogs.com/liaoyu/p/oracle-procedure-batch-insert.html 作者:L君还在说之乎者也 最近在工作中,需要使用生成一些 ...

  6. mysql存储过程批量插入数据

    DROP TABLE IF EXISTS TeachersInfo; CREATE TABLE TeachersInfo ( id INT NOT NULL AUTO_INCREMENT, teach ...

  7. postgres 使用存储过程批量插入数据

    參考资料(pl/pgsql 官方文档): http://www.postgresql.org/docs/9.3/static/plpgsql.html create or replace functi ...

  8. C#批量插入数据到Sqlserver中的四种方式

    我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...

  9. C#批量插入数据到Sqlserver中的三种方式

    本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...

随机推荐

  1. 几个精彩的DMV

    --统计表的增删改次数,反映表的使用程度 SELECT DB_NAME([database_id]) AS [Database] ,iops.[object_id] AS [ObjectID] ,QU ...

  2. linux服务器性能优化

    1.这里的吞吐率特指Web服务器单位时间内处理的请求.       2.压力测试的前提:1>并发用户数 2>总请求数 3>请求资源描述       3.用户平均请求等待时间主要用户衡 ...

  3. TextToSpeech之阅读文字

    创建阅读类 /** * Created by RongGuang on 2014-11-21. * 中文朗读 */ public class ChineseToSpeech { private Tex ...

  4. Cordova for Android(Windows)环境配置

    PS:注意事项 一些坑在此声明: 1.安装Eclipse后,记得设置各项编码格式为utf-8 请移步:http://www.blogjava.net/xiaomage234/archive/2014/ ...

  5. php中rsa加密解密验证

    RSA非对称加密,对敏感的数据传输进行数据加密.验证等.测试环境:wamp.aliyun虚拟主机(lamp)一.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以).下载生 ...

  6. linux查看修改线程默认栈空间大小(ulimit -s)

    linux查看修改线程默认栈空间大小 ulimit -s 1.通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2.通过命令 ulimit -s 设置大 ...

  7. GMF中,删除节点和连线的另一种实现

    问题 在GMF中,如果需要programmatically删除节点或连线,在google中我们很容易搜索到<GMF中,删除节点和连线的实现>一文(我并不确定这是原创作者的原始链接),很多人 ...

  8. iOS,Xcode7 制作Framework,含资源和界面

    Xcode7 制作Framework  本文通过Demo方式介绍1)将含bundle和存代码编写界面打包进framework:2)将storyboard +assets.xcassets打包. (一) ...

  9. 常用js总结1

    1.cookie.js(封装了cookie的基本操作) 1.引入cookie.js <script type="text/javascript" src="../j ...

  10. 【转】对硬盘进行分区时,GPT和MBR区别。

    在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区.GPT是一种新的标准,并在逐渐取代MBR. GPT带来了很多新特性,但MBR仍然拥有最好的兼容性.GPT并不是W ...