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. [SQL_Server_Question]Msg 1105无法为数据库 'tempdb' 中的对象分配空间,因为 'PRIMARY' 文件组已满

    错误消息: Msg 1105, Level 17, State 2, Line 266Could not allocate space for object 'dbo.Large Object Sto ...

  2. Mininet VM设置笔记

    Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...

  3. Go学习指南

    学习Golang书籍&资料: 1. The Go Programming Language Specification:  http://golang.org/ref/spec 2. How ...

  4. 微软职位内部推荐-Senior Dev Lead

    微软近期Open的职位: Bing Index Serve team is hiring! We are one of the core teams in Bing serving more than ...

  5. 微软职位内部推荐-Senior SDE for Windows App Experience

    微软近期Open的职位: Job posting title: Senior Software Development Engineer Location: China, Beijing Divisi ...

  6. Week1 Team Homework #2: Introduction of each team member

    王洛书 我是来自浙江的王洛书.热爱历史,热爱美食,热爱代码,热爱博物馆.很喜欢软件工程这门课的上课方式,也很喜欢组里的这些同学.希望能大家一起努力,在这门课上真正地收获能力上的提升!   陈睿翊

  7. 传说中的Markov"不过如此”

    因为看一篇题为 Passive Measurement of Interference in WiFi Network with Application in Misbehavior Detectio ...

  8. 3139:[HNOI2013]比赛 - BZOJ

    题目描述 Description 沫沫非常喜欢看足球赛,但因为沉迷于射箭游戏,错过了最近的一次足球联赛.此次联赛共N只队伍参加,比赛规则如下: (1) 每两支球队之间踢一场比赛. (2) 若平局,两支 ...

  9. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  10. SQL SERVER其它函数

    本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER常用的其它函数.(其它数据库这里就不罗列了,想看更多的可以关注<程序员的SQL金典>). 具 ...