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. MySQL创建复合索引

    在MySQL数据库中,创建复合索引的时候,不知道在创建过程中哪个列在前面,哪个列该在后面,用以下方式即可: select count(distinct first_name)/count(*) as ...

  2. 删除或清空具有外键约束的表数据报-ERROR 1701 (42000)

    OS:  centos 6.3 DB:5.5.14 mysql> select database();+------------+| database() |+------------+| sa ...

  3. ORA-1653: unable to extend table SYS.AUD$

    今早运维组的同事反映有个系统功能很多地方都报错,怀疑是不是数据库有什么问题.于是登录数据库检查,通过crsctl status res -t检查,发现所有集群资源都是OK的,没有哪个资源挂掉了.于是到 ...

  4. python之setattr,getattr,hasattr

    可以使用setattr(), getattr(), hasattr()动态对实例进行操作. 相当于Java中的反射机制, 或者更确切地, 像JavaScript中属性操作. 具体属性: __dict_ ...

  5. 从零开始学ios开发(十四):Navigation Controllers and Table Views(上)

    这一篇我们将学习一个新的控件Navigation Controller,很多时候Navigation Controller是和Table View紧密结合在一起的,因此在学习Navigation Co ...

  6. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  7. mysql注入绕过的一些技巧

    虽然mysql + php的开发中可以使用pdo中,但是有些老久的程序没有使用,或其他原因 1.注释绕过 select/*comment*/user/*zzsdsdsf*/from mysql.use ...

  8. System.IO.StreamWriter

    string path = @"D:\a.txt"; System.IO.StreamWriter swOut = new System.IO.StreamWriter(path, ...

  9. 2729:[HNOI2012]排队 - BZOJ

    题目描述 Description某中学有n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人 ...

  10. 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别

    一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...