sql 存储过程 循环使用
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 存储过程 循环使用的更多相关文章
- sql存储过程循环实现事务
//往一张表中添加数据,获取添加数据生成的ID,再往另一张表中添加多条数据 ALTER PROCEDURE [dbo].[AttendanceCardAndDetail_Add] @SchoolID ...
- SQL存储过程+游标 循环批量()操作数据
本人收集的,挺有用的 1. 利用游标循环更新.删除MemberAccount表中的数据 DECLARE My_Cursor CURSOR --定义游标 FOR (SELECT * FROM dbo.M ...
- sql存储过程中循环批量插入
前几天有一个需求很头痛,部门是有上下级关系的,在给部门的经理赋予角色和权限的时候,通常我们都会认为假如经理A的部门是1,那么我给了他部门1 的管理权限,那么1的下级部门101,102,103 &quo ...
- PL/SQL存储过程编程
PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...
- SQL 存储过程(学生,课程表,选修表)
SQL 存储过程(学生,课程表,选修表) 一.存储过程的分类 在SQL Server中存储过程分过两类: 1)系统存储过程("sp_"作为前缀) 2)用户自定义存储过程 二.创建和 ...
- MySQL 存储过程循环
MySQL 存储过程循环 MySQL循环语句(包括WHILE,REPEAT和LOOP)来根据条件反复运行代码块. MySQL提供循环语句,允许您根据条件重复执行一个SQL代码块. MySQL中有三个 ...
- 《软件测试自动化之道》读书笔记 之 SQL 存储过程测试
<软件测试自动化之道>读书笔记 之 SQL 存储过程测试 2014-09-28 待测程序测试程序 创建测试用例以及测试结果存储 执行T-SQL脚本 使用BCP工具导入测试用例数据 ...
- SQL存储过程分页(通用的拼接SQL语句思路实现)
多表通用的SQL存储过程分页 案例一: USE [Community] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Scrip ...
- SQL存储过程的调用及写法
调用函数: public class SqlProcess { ; public DataSet ReturnSet = null; public SqlDataAdapter adapter = n ...
随机推荐
- Python之str(),repr(),``
对于对象obj: str()生成的字串是给人看的 repr()生成的字串是给解析器看的 ``与repr()等义. 最直接就是: ------------------- obj=eval(repr(ob ...
- python之函数嵌套
python很多特性与JavaScript是相似甚至相同的: 1. 无类型 2. 函数亦对象 .... 自然: python也允许函数嵌套, 这与JavaScript中函数闭包的作用一样....
- sysfs->sys简单介绍
Sys节点 1:sysfs 是 Linux 内核中设计较新的一种虚拟的基于内存的文件系统, sysfs 的挂载点 /sys 目录结构. 2:/sys 文件系统下的目录结构 /sys 下的目录结构是经过 ...
- thinkphp join 查询
$user=M('user')->table(C('DB_PREFIX').'user as a')->join(C('DB_PREFIX').'role_user as b on a.u ...
- Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'(using password: YSE)
安装mysql后,使用命令登录mysql居然报错了,Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'(using ...
- linux驱动之I2C
include/linux/i2c.h struct i2c_msg;struct i2c_algorithm;struct i2c_adapter;struct i2c_client;struct ...
- C#系统缓存全解析
原文:http://blog.csdn.net/wyxhd2008/article/details/8076105 目录(?)[-] 系统缓存的概述 页面输出缓存 页面局部缓存 文件缓存依赖 数据库缓 ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
- 【模板】Big-Step-Giant-Step 大步小步
求一个 的最小整数解 bsgs 当h是质数的时候使用 extbsgs 不满足上面那种情况的时候 具体参见http://tonyfang.is-programmer.com/posts/178997.h ...
- lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...