USE [clab]
GO
/****** Object: StoredProcedure [dbo].[sp_bd_getResultByEcd] Script Date: 08/06/2014 16:47:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: zhangcy
-- Create date: 2014-07-09
-- Description: 传入病人ID及项目代码获取项目最新的检验结果
-- =============================================
CREATE PROCEDURE [dbo].[sp_bd_getResultByEcd]
(
@pat_in_no varchar(50) --病人id
,@itm_id_list varchar(500) --项目编码字符串,各项目编码以,进行分割
)
AS Declare @NeedParse varchar(500) --参数 没有处理的字符串 if (CharIndex(',', @itm_id_list)=0)
BEGIN
SELECT
a.res_itm_id as 项目id
,a.res_id as 报告id
,a.res_itm_ecd as 项目代码
,a.res_chr as 结果
,a.res_date as 检测时间
,a.value as 提示
from
(SELECT row_number() over (partition by res_itm_id order by res_date DESC) as rownum
,pat_in_no
,res_id
,res_itm_id
,res_itm_ecd
,res_chr
,res_date
,value
from patients with(nolock)
left join resulto with(nolock) on patients.pat_id=resulto.res_id
left join dict_res_ref_flag on dict_res_ref_flag.id = resulto.res_ref_flag
where pat_in_no=@pat_in_no
and pat_flag in (2,4)
and res_itm_id=@itm_id_list) as a
where a.rownum=1
END
else
BEGIN
set @NeedParse =@itm_id_list
while (charIndex(',', @NeedParse)>0)
begin
SELECT
a.res_itm_id as 项目id
,a.res_id as 报告id
,a.res_itm_ecd as 项目代码
,a.res_chr as 结果
,a.res_date as 检测时间
,a.value as 提示
from
(SELECT row_number() over (partition by res_itm_id order by res_date DESC) as rownum
,pat_in_no
,res_id
,res_itm_id
,res_itm_ecd
,res_chr
,res_date
,value
from patients with(nolock)
left join resulto with(nolock) on patients.pat_id=resulto.res_id
left join dict_res_ref_flag on dict_res_ref_flag.id = resulto.res_ref_flag
where pat_in_no=@pat_in_no
and pat_flag in (2,4)
and res_itm_id=SubString(@NeedParse,1,CharIndex(',',@NeedParse)-1)) as a
where a.rownum=1 set @NeedParse =SubString(@NeedParse,CharIndex(',', @NeedParse)+1,len(@NeedParse)-CharIndex(',', @NeedParse))
end SELECT
a.res_itm_id as 项目id
,a.res_id as 报告id
,a.res_itm_ecd as 项目代码
,a.res_chr as 结果
,a.res_date as 检测时间
,a.value as 提示
from
(SELECT row_number() over (partition by res_itm_id order by res_date DESC) as rownum
,pat_in_no
,res_id
,res_itm_id
,res_itm_ecd
,res_chr
,res_date
,value
from patients with(nolock)
left join resulto with(nolock) on patients.pat_id=resulto.res_id
left join dict_res_ref_flag on dict_res_ref_flag.id = resulto.res_ref_flag
where pat_in_no=@pat_in_no
and pat_flag in (2,4)
and res_itm_id=@NeedParse) as a
where a.rownum=1 END
GO

此存储过程为:传入病人ID,项目编码集,其中各编码以逗号分割。

存储过程里面要做的就是循环读取项目编码集里面的单个编码,及结合病人ID获取该病人及该项目的检验结果

sql 存储过程 循环使用的更多相关文章

  1. sql存储过程循环实现事务

    //往一张表中添加数据,获取添加数据生成的ID,再往另一张表中添加多条数据 ALTER PROCEDURE [dbo].[AttendanceCardAndDetail_Add] @SchoolID ...

  2. SQL存储过程+游标 循环批量()操作数据

    本人收集的,挺有用的 1. 利用游标循环更新.删除MemberAccount表中的数据 DECLARE My_Cursor CURSOR --定义游标 FOR (SELECT * FROM dbo.M ...

  3. sql存储过程中循环批量插入

    前几天有一个需求很头痛,部门是有上下级关系的,在给部门的经理赋予角色和权限的时候,通常我们都会认为假如经理A的部门是1,那么我给了他部门1 的管理权限,那么1的下级部门101,102,103 &quo ...

  4. PL/SQL存储过程编程

    PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...

  5. SQL 存储过程(学生,课程表,选修表)

    SQL 存储过程(学生,课程表,选修表) 一.存储过程的分类 在SQL Server中存储过程分过两类: 1)系统存储过程("sp_"作为前缀) 2)用户自定义存储过程 二.创建和 ...

  6. MySQL 存储过程循环

    MySQL  存储过程循环 MySQL循环语句(包括WHILE,REPEAT和LOOP)来根据条件反复运行代码块. MySQL提供循环语句,允许您根据条件重复执行一个SQL代码块. MySQL中有三个 ...

  7. 《软件测试自动化之道》读书笔记 之 SQL 存储过程测试

    <软件测试自动化之道>读书笔记 之 SQL 存储过程测试 2014-09-28 待测程序测试程序   创建测试用例以及测试结果存储  执行T-SQL脚本  使用BCP工具导入测试用例数据  ...

  8. SQL存储过程分页(通用的拼接SQL语句思路实现)

    多表通用的SQL存储过程分页 案例一: USE [Community] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Scrip ...

  9. SQL存储过程的调用及写法

    调用函数: public class SqlProcess { ; public DataSet ReturnSet = null; public SqlDataAdapter adapter = n ...

随机推荐

  1. c++各种数据类型表示范围

    符号属性     长度属性     基本型     所占位数     取值范围       输入符举例      输出符举例 --            --          char        ...

  2. 这些年,我收集的JavaScript代码

    这些年,我收集的JavaScript代码(一) http://www.cnblogs.com/jscode/archive/2012/07/25/2605395.html 这些年,我收集的JavaSc ...

  3. 问题 K: 【USACO2012Feb】植草 {Bronze题2}

    按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个..只能想到这个了 ; type node=record l,r,mid,sum,delta: ...

  4. LintCode-Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  5. iOS刷新第三方MJRefresh的基本使用

    iOS开发中最好用的刷新第三方框架 MJRefresh GitHub : https://github.com/CoderMJLee/MJRefresh UIRefreshControl的介绍 1,U ...

  6. aes 解密出现 java.lang.NumberFormatException: Invalid int: "ch"

    原因: 将加密/解密的seed 和 加密内容顺序放反.  decrypt(String seed, String encrypted) 附上AES解密/加密代码(android开发): package ...

  7. javascript基础之客户端事件驱动

    我们知道,面向对象发展起来后,“一夜之间”,几乎所有的语言都能基于对象了,JavaScript也是基于对象的语言.用户在浏览器上的行为称作“事件”,之后引发的一系列动作,比如弹窗啦,改变浏览器大小啦, ...

  8. 移动端material风格日期时间选择器

    原文 好多时候在移动端需要一个的日期选择器,由于在应用上有可能应用各种框架库(Vue.js, React.js, zepto.js等):所以说一个无依赖的,这样易于上层进行封装.直接开门见山,先来张动 ...

  9. uva 11174

    刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...

  10. 分享: 利用Readability解决网页正文提取问题

    原文:http://www.cnblogs.com/iamzyf/p/3529740.html 做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以 ...