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 =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'

下面是成功插入后查詢到的結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM