Sql server 存储过程批量插入若干数据。
测试时,经常需要生成大量数据来测试系统性能,此功能可以用存储过程快速生成。
1. 随机生成日期
DECLARE @Date_start datetime
DECLARE @Date_end datetime
SET @Date_start= '1930-01-01'
SET @Date_end=getdate()
select @birthDate=dateadd(minute,abs(checksum(newid()))%(datediff(minute,@Date_start,@Date_end)+1),@Date_start)
2. 随机从给定的若干值中挑选一个(例如随机生成性别)
DECLARE @sex NVARCHAR(10)
SET @sex= CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @sex=''
SET @sex='Male';
ELSE IF
@sex=''
SET @sex='Female';
ELSE IF @sex=''
SET @sex=NULL;
3. 生成编号
DECLARE @subCode_base NVARCHAR(30)
DECLARE @barcode NVARCHAR(30)
SET @subCode_base='AutoSubCode_'
SET @subCode=@subCode_base+CONVERT(NVARCHAR,@index)
4. 单表插入存储过程
CREATE PROCEDURE [dbo].[add_SubjectInfo]
AS
DECLARE @subCode_base NVARCHAR(30)
DECLARE @barcode_base NVARCHAR(30)
DECLARE @birthDate datetime
DECLARE @sex NVARCHAR(10)
DECLARE @fullName_base NVARCHAR(30)
DECLARE @mortalityStatus NVARCHAR(30)
DECLARE @reserved NVARCHAR(10)
DECLARE @recordCreateDate datetime
DECLARE @recordCreator INTEGER DECLARE @count INTEGER
DECLARE @index INTEGER DECLARE @subCode NVARCHAR(30)
DECLARE @barcode NVARCHAR(30)
DECLARE @fullName NVARCHAR(30) DECLARE @Date_start datetime
DECLARE @Date_end datetime SET @subCode_base='AutoSubCode_'
SET @barcode_base='AutoBM_' SET @Date_start= '1930-01-01'
SET @Date_end=getdate() SET @fullName_base='AutoFullName_'
SET @recordCreateDate=GETDATE()
SET @recordCreator=22
-- 调整生成的条数=@count-@index
SET @count=10
SET @index=1 WHILE @index<@count
BEGIN
-- 生产编号
SET @subCode=@subCode_base+CONVERT(NVARCHAR,@index)
SET @barcode=@barcode_base+CONVERT(NVARCHAR,@index)
SET @fullName=@fullName_base+CONVERT(NVARCHAR,@index) -- 随机生成性别 Male/Female/空
SET @sex= CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @sex=''
SET @sex='Male';
ELSE IF
@sex=''
SET @sex='Female';
ELSE IF @sex=''
SET @sex=NULL; -- 随机生成存活状态 Dead/Alive/空
SET @mortalityStatus = CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @mortalityStatus=''
SET @mortalityStatus='Dead';
ELSE IF
@mortalityStatus=''
SET @mortalityStatus='Alive';
ELSE IF @mortalityStatus=''
SET @mortalityStatus=NULL; -- 随机生成Reserved状态 Yes/No/空
SET @reserved = CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @reserved=''
SET @reserved='Yes';
ELSE IF
@reserved=''
SET @reserved='No';
ELSE IF @reserved=''
SET @reserved=NULL;
-- 随机生产日期
select @birthDate=dateadd(minute,abs(checksum(newid()))%(datediff(minute,@Date_start,@Date_end)+1),@Date_start) INSERT INTO subject(subject_code,barcode, birth_date,sex, full_name,mortality_status,reserved, record_create_date,record_creator)
VALUES (@subCode, @barcode,@birthDate,@sex,@fullName,@mortalityStatus,@reserved,@recordCreateDate,@recordCreator) SET @index=@index+1
END
5 多表插入存储过程
USE [bio-d]
GO
/****** Object: StoredProcedure [dbo].[add_SubjectAndSubjectStudyInfo] Script Date: 2018/8/23 14:30:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[add_SubjectAndSubjectStudyInfo]
AS
-- 公用参数 DECLARE @subid_index INTEGER -- 获取Subject 表的最大id+1 作为添加的患者编号后缀起点
DECLARE @count INTEGER -- 用户作为循环的跳出条件
DECLARE @insertRow INTEGER -- 一次需要插入的条数 SET @insertRow=10000
-- 调整生成的条数=@count-@index
SET @subid_index=((select max(id) from subject)+1) --需要更换为动态的
SET @count=@subid_index+@InsertRow -- 插入Subject表
DECLARE @sub_code_base NVARCHAR(30)
DECLARE @sub_barcode_base NVARCHAR(30)
DECLARE @sub_birthdate DATETIME
DECLARE @sub_sex NVARCHAR(10)
DECLARE @sub_fullName_base NVARCHAR(30)
DECLARE @sub_mortalityStatus NVARCHAR(30)
DECLARE @sub_reserved NVARCHAR(10)
DECLARE @sub_recordCreateDate DATETIME
DECLARE @sub_recordCreator INTEGER DECLARE @sub_code NVARCHAR(30)
DECLARE @sub_barcode NVARCHAR(30)
DECLARE @sub_fullName NVARCHAR(30) DECLARE @Date_start DATETIME
DECLARE @Date_end DATETIME SET @sub_code_base='AutoSubCode_'
SET @sub_barcode_base='AutoBM_' SET @Date_start= '1930-01-01'
SET @Date_end=getdate() SET @sub_fullName_base='AutoFullName_'
SET @sub_recordCreateDate=GETDATE()
SET @sub_recordCreator=22 -- 插入Subject_study表
DECLARE @study_id INTEGER SET @study_id=10082 -- 插入Biomaterial表
DECLARE @bio_barcode_base NVARCHAR(30)
DECLARE @bioName_base NVARCHAR(30)
DECLARE @bio_recordCreateDate datetime
DECLARE @bio_recordCreator INTEGER DECLARE @bio_barcode NVARCHAR(30)
DECLARE @bio_bioName NVARCHAR(30) SET @bio_barcode_base='AutoBioBM_'
SET @bioName_base='AutoBioName_' -- 插入biomaterial_study表
DECLARE @biomaterial_id INTEGER
SET @biomaterial_id=(select max(id) from biomaterial)+1 WHILE @subid_index<@count
BEGIN
-- 生产编号
SET @sub_code=@sub_code_base+CONVERT(NVARCHAR,@subid_index)
SET @sub_barcode=@sub_barcode_base+CONVERT(NVARCHAR,@subid_index)
SET @sub_fullName=@sub_fullName_base+CONVERT(NVARCHAR,@subid_index) -- 随机生成性别 Male/Female/空
SET @sub_sex= CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @sub_sex=''
SET @sub_sex='Male';
ELSE IF
@sub_sex=''
SET @sub_sex='Female';
ELSE IF @sub_sex=''
SET @sub_sex=NULL; -- 随机生成存活状态 Dead/Alive/空
SET @sub_mortalityStatus = CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @sub_mortalityStatus=''
SET @sub_mortalityStatus='Dead';
ELSE IF
@sub_mortalityStatus=''
SET @sub_mortalityStatus='Alive';
ELSE IF @sub_mortalityStatus=''
SET @sub_mortalityStatus=NULL; -- 随机生成Reserved状态 Yes/No/空
SET @sub_reserved = CONVERT(NVARCHAR,cast( RAND()*3 as int))
IF @sub_reserved=''
SET @sub_reserved='Yes';
ELSE IF
@sub_reserved=''
SET @sub_reserved='No';
ELSE IF @sub_reserved=''
SET @sub_reserved=NULL;
-- 随机生产日期
select @sub_birthdate=dateadd(minute,abs(checksum(newid()))%(datediff(minute,@Date_start,@Date_end)+1),@Date_start) -- 插入Subject表
INSERT INTO subject(subject_code,barcode, birth_date, sex, full_name,mortality_status,reserved, record_create_date,record_creator)
VALUES (@sub_code, @sub_barcode,@sub_birthdate,@sub_sex,@sub_fullName,@sub_mortalityStatus,@sub_reserved,@sub_recordCreateDate,@sub_recordCreator) -- 插入Subject_study表
INSERT INTO subject_study(subject_id,study_id)
VALUES (@subid_index,@study_id) -- 插入Biomaterial表
-- 生产编号
SET @bio_barcode=@bio_barcode_base+CONVERT(NVARCHAR,@subid_index)
SET @bio_bioName=@bioName_base+CONVERT(NVARCHAR,@subid_index)
INSERT INTO biomaterial(at_facility,bar_code, batch_id, biomaterial_name,carrier,concentration,concentration_unit1,container_type,created_date, current_status, external_id,external_source, mass,mass_units,parent_id, storage_location,subject_id, tracking_number,volume,volume_units,notes,record_create_date, record_creator, concentration_unit2)
VALUES ( 2, @bio_barcode ,'', @bio_bioName, NULL, '', '', 1, NULL, 'In Inventory', '', '', '', '', -1 ,'', @subid_index,'', '', '', '', @bio_recordCreateDate,@bio_recordCreator,''); -- 插入biomaterial_study 表
INSERT INTO biomaterial_study(study_id,biomaterial_id)
VALUES(@study_id,@biomaterial_id) -- 插入样本和附件的关联
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,220)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,221)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,236)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,237)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,251)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,252)
INSERT INTO attachment_associated(type,belong_id,attachment_id) values(2,@biomaterial_id,253)
SET @subid_index=@subid_index+1
END
Sql server 存储过程批量插入若干数据。的更多相关文章
- SQL Server TVPs 批量插入数据
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...
- SQL Server 2008 批量插入数据时报错
前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...
- sql server中批量插入与更新两种解决方案分享(存储过程)
转自http://www.shangxueba.com/jingyan/1940447.html 1.游标方式 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONG ...
- sql server中批量插入与更新两种解决方案分享
若只是需要大批量插入数据使用bcp是最好的,若同时需要插入.删除.更新建议使用SqlDataAdapter我测试过有很高的效率,一般情况下这两种就满足需求了 bcp方式 复制代码 代码如下: /// ...
- SQL SERVER数据库批量替换某个数据表里的数据update
批量替换:将A表CMC里面所有包含a替换成b而不影响其他内容UPDATE A SET CMC=REPLACE(CMC,'a','b')
- SQL Server ->> 存储过程sp_rename重命名数据对象
1) 表转移Schema和重命名表 ALTER SCHEMA Stage TRANSFER dbo.Stage_AAA; EXEC sp_rename 'Stage.Stage_AAA', 'AAA' ...
- Oracle 存储过程批量插入数据
oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...
- 使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历
原文:使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 常常遇到需要向SQL Server插入批量数据,然后在存储过程中对这些数据进行进一步处理的情况.存储过 ...
- 使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历
使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 原文:使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 常常遇 ...
随机推荐
- LeetCode 806 Number of Lines To Write String 解题报告
题目要求 We are to write the letters of a given string S, from left to right into lines. Each line has m ...
- jmeter发送https请求
- LED客显的类
using System; using System.IO.Ports; namespace Common { public class LedHelper { ; private static st ...
- laravel用crud之index列出产品items
前面我们说了laravel用crud修改产品items-新建resource controller和routing,现在我们要把产品items罗列出来,需要修改路由和模板,一起随ytakh来看看把 1 ...
- 20180323 DataTable增加DataRow方式优化
1. 我开始开发程序时,很多问题考虑不周期,不断的在改进中 最开始我的写法,创建一个DataTable 的Columns,采用语句 dt2.Columns.Add("ID"); 这 ...
- MHA配置参数详解 【转】
mha配置参数详解: 参数名字 是否必须 参数作用域 默认值 示例 hostname Yes Local Only - hostname=mysql_server1, hostname=192.168 ...
- 小程 序swiper自适应宽高
https://blog.csdn.net/qq_31604363/article/details/73715944 小程 序swiper自适应宽高 小程 序swiper自适应宽高
- spark提交任务的三种的方法
在学习Spark过程中,资料中介绍的提交Spark Job的方式主要有三种: 第一种: 通过命令行的方式提交Job,使用spark 自带的spark-submit工具提交,官网和大多数参考资料都是已这 ...
- Spark SQL 函数全集
org.apache.spark.sql.functions是一个Object,提供了约两百多个函数. 大部分函数与Hive的差不多. 除UDF函数,均可在spark-sql中直接使用. 经过impo ...
- 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)
Given a linked list, remove the n-th node from the end of list and return its head. Example: ...