测试时,经常需要生成大量数据来测试系统性能,此功能可以用存储过程快速生成。

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 存储过程批量插入若干数据。的更多相关文章

  1. SQL Server TVPs 批量插入数据

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  2. SQL Server 2008 批量插入数据时报错

    前几天在SQL Server 2008同步产品数据时,总是提示二进制文本被截断的错误,但是经过检查发现数据都符合格式要求. 百思不得其解,单独插入一条条数据则可以插入,但是批量导入则报错. 批量导入代 ...

  3. sql server中批量插入与更新两种解决方案分享(存储过程)

    转自http://www.shangxueba.com/jingyan/1940447.html 1.游标方式 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONG ...

  4. sql server中批量插入与更新两种解决方案分享

    若只是需要大批量插入数据使用bcp是最好的,若同时需要插入.删除.更新建议使用SqlDataAdapter我测试过有很高的效率,一般情况下这两种就满足需求了 bcp方式 复制代码 代码如下: /// ...

  5. SQL SERVER数据库批量替换某个数据表里的数据update

    批量替换:将A表CMC里面所有包含a替换成b而不影响其他内容UPDATE A SET CMC=REPLACE(CMC,'a','b')

  6. SQL Server ->> 存储过程sp_rename重命名数据对象

    1) 表转移Schema和重命名表 ALTER SCHEMA Stage TRANSFER dbo.Stage_AAA; EXEC sp_rename 'Stage.Stage_AAA', 'AAA' ...

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

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

  8. 使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历

    原文:使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 常常遇到需要向SQL Server插入批量数据,然后在存储过程中对这些数据进行进一步处理的情况.存储过 ...

  9. 使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历

    使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历   原文:使用XML向SQL Server 2005批量写入数据——一次有关XML时间格式的折腾经历 常常遇 ...

随机推荐

  1. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  2. Appium入门(7)__Appium Desired Capabilities

    Desired Capabilities 是由多个键值对组成,代表移动设备相关信息.由Appium Client向Appium Server发送. 但无论Appium Client使用何种语言,最终是 ...

  3. 九九乘法表python3写入文件中

    写入文件代码如下: with open("e:\\test01.txt","w+",encoding="utf-8") as wq: for ...

  4. python接口测试实例--数据驱动(程序与数据分离)

    #encoding=utf-8import requestsimport jsonimport osimport hashlibimport picklefrom conf import * stat ...

  5. Vue双向数据绑定原理分析(转)

    add by zhj: 目前组里使用的是前端技术是jQuery + Bootstrap,后端使用的Django,Flask等,模板是在后端渲染的.前后端没有分离,这种做法有几个缺点 1. 模板一般是由 ...

  6. 用uart实现printf函数

    硬件:JZ2440 实现功能:用putchr()函数实现printf() start.s nand.c uart.c uart.h my_stdio.c my_stdio.h main.c start ...

  7. 【JMeter】JMeter如何输出测试报告

    环境要求 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚本文件 基本操作 1:在你的脚本文件路径下,执行cmd命令:jmeter -n -t tes ...

  8. 使用RegisterNatives注冊原生代码

    在Android开发本地代码时,有两种方式.一种是使用javah生成头文件.然后编辑源码,还有一种不用生成头文件,直接编辑代码后,使用RegisterNatives方法进行注冊,以下是一个Demo: ...

  9. JSONObject,String,Map互相转换

    JSONObject和String相互转换 JSONObject jsonObject = new JSONObject(); JSONArray jsonArray = new JSONArray( ...

  10. Java面试总结(面试流程及核心面试题)

    Java面试流程及核心面试题 面试整体流程 1.1 简单的自我介绍      我是xxxx,工作xxx年.我先后在xxxx公司.yyyy公司工作.先后做个xxxx项目.yyyy项目. 1.2 你简单介 ...