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

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 771 Jewels and Stones 解题报告

    题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...

  2. jquery中选取兄弟节点的方法

    $('#id').siblings() 当前元素所有的兄弟节点$('#id').prev() 当前元素前一个兄弟节点$('#id').prevaAll() 当前元素之前所有的兄弟节点$('#id'). ...

  3. 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  4. SpringBoot-区分不同环境配置文件

    spring.profiles.active=pre application-dev.properties:开发环境 application-test.properties:测试环境 applicat ...

  5. 如何快速REPAIR TABLE

    早上到公司,刚准备吃早餐,手机响了,一看是服务器自动重启了.好吧,准备修复数据吧.游戏服的游戏日志使用的是MyISAM.众所周知,MyISAM表在服务器意外宕机或者mysqld进程挂掉以后,MyISA ...

  6. python json 模块

    什么是json? json是返回的是字符串格式,把python数据类型列表.字典转换成json字符串格式, 这种格式java php 其他语言都可以认识的字符串,可以跨语言交流. json,用于字符串 ...

  7. 如何卸载docker

    1.卸载 (1)yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ d ...

  8. 学习Shell(二)变量

    如何给shell脚本传入参数 1.执行“vi test.sh”创建一个新的shell脚本. vi test.sh 2.脚本test.sh的内容如下: #!/bin/sh name=$ echo &qu ...

  9. vue 获取屏幕宽高 width height

    /**  * 获取屏幕宽高  */ Vue.prototype.getViewportSize = function(){   return {     width: window.innerWidt ...

  10. [py]初始化dict结构和json.dump使用

    1.json.dump使用 http://python3-cookbook.readthedocs.io/zh_CN/latest/c06/p02_read-write_json_data.html ...