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

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] [intNOT NULL,
    [DetailName] [nvarchar](50) NOT NULL,
    [DetailAge] [intNOT 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. Eclipse中为自己写完的函数添加注释(快捷键ALT+SHIFT+J)

    函数名上右键:Source--->Generate Element Comment

  2. windows系统调用 利用事件对象实现进程通信

    #include "iostream" #include "windows.h" #include "cstring" using name ...

  3. 夺命雷公狗-----React---5--props对象的传递

    提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  4. 将序列化成json格式的日期(毫秒数)转成日期格式

    <script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...

  5. css样式控制鼠标滑过显示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. ADT中通过Android SDK Manager去安装x86的image时无法下载

    参考:http://www.crifan.com/adt_android_sdk_manager_can_not_download_package_x86_image/

  7. IOS UIView 属性clipsToBounds

    当一个view上加了一个SubView 并且subview 的size超出了view的size那么默认超出部分也会显示出来,要想不显示出来设置 view的clipsToBounds属性设置成YES:即 ...

  8. Flowplayer-playlist

    SOURCE URL: https://flowplayer.org/docs/playlist.html HTML layout Here is a typical setup for a play ...

  9. for与if、else的使用

    #!/usr/bin/env python def fun3(x) : num =0 for i in x : if i == "" : num += 1 if num == 0 ...

  10. Maven学习(二) -- 坐标和依赖

    标签(空格分隔): 学习笔记 坐标 实际就像在几何中,我们用一对坐标(x, y)来表示坐标系中唯一的点:或者我们可以用(经度,纬度)来表示地球上的某一个位置,在Maven的世界中,有坐标来唯一的表示项 ...