USE [Nursing] GO /****** Object: StoredProcedure [dbo].[P_GetICUVitualSign] Script Date: 05/21/2015 21:47:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[P_GetICUVitualSign] @VisitNumberParam varchar (30), @RecordTypeParam datetime AS BEGIN SELECT * into #temp FROM [Nursing].[dbo].[V_ICUVitualSign] where [email protected] order by RecordTime -----------------------2----------------- create table #tempdata ( visitnumber varchar(30), recordtime datetime, code varchar(100), value varchar(100) ) create table #tempdata2 ( rowindex int, visitnumber varchar(30), recordtime datetime, code varchar(100), value varchar(100) ) -----------------------3----------------- declare mycursor cursor for select VisitNumber,ItemNo,Value,RecordTime from #temp declare @VisitNumber varchar(100) declare @ItemNo varchar(100) declare @Value varchar(100) declare @RecordTime datetime open mycursor FETCH next from mycursor into @VisitNumber,@ItemNo,@Value,@RecordTime if @ItemNo=0 begin insert into #tempdata2(rowindex,visitnumber,code,value) select pos as rowindex, @VisitNumber as visitnumber, nvalue as code ,‘‘ as value from Nursing.dbo.SplitToTable(@Value) where LEN(nvalue)>0 end while @@FETCH_STATUS=0 begin FETCH next from mycursor into @VisitNumber,@ItemNo,@Value,@RecordTime declare @count int select @count=COUNT(1) from #tempdata2 if(@ItemNo=0) begin delete from #tempdata2 insert into #tempdata2(rowindex,visitnumber,code,value) select pos as rowindex, @VisitNumber as visitnumber, nvalue as code ,‘‘ as value from Nursing.dbo.SplitToTable(@Value) where LEN(nvalue)>0 end else begin if(@count>0) begin delete from #tempdata where [email protected] insert into #tempdata( visitnumber, recordtime , code , value ) select a.visitnumber as visitnumber, b.recordtime as recordtime , a.code as code , b.value as value from #tempdata2 as a left join (select pos as rowindex, @RecordTime as recordtime, nvalue as value from Nursing.dbo.SplitToTable(@Value) where LEN(nvalue)>0) as b on a.rowindex=b.rowindex end end end close mycursor DEALLOCATE mycursor -----------------------return data----------------- select ltrim(rtrim(visitnumber)) as visitnumber, recordtime as recordtime, ltrim(rtrim(code )) as code, ltrim( rtrim( value )) as value from #tempdata where [email protected] ------------------------------over----------------- drop table #temp drop table #tempdata drop table #tempdata2 END GO
时间: 2024-12-29 02:38:22