gouri
Starting Member
7 Posts |
Posted - 2008-06-11 : 05:07:21
|
I have a server application which is working as a simulator for data generation. It generates data and immediately sends It to a queue in service broker. The messages in service broker queue need to be read every one minute (one minute push to database) and sent to the table in database. The data has various patient ids and bed numbers. In table if there is no record with the same patient id and bed number a new record gets added.If the patient id and bed number exists then it is checked if the difference between the time in the existing record and new data is less than 10 minutes. If so the same record is updated. Otherwise a new record gets added.I am having each record with 10 minutes of data.What will be the optimum way to achieve this?This is the code I am using--There is a queue named ReceiverQueue--table structure is bedno char(10),pid char(10),--starttime datetime, hrtype char(10), v textwhile (1=1)BEGINDECLARE @message XMLDECLARE @PKey nchar(10)DECLARE @BKey nchar(10)DECLARE @StartTime datetimeDECLARE @HKey nchar(10)DECLARE @vKey nvarchar(12)begin transaction;waitfor (RECEIVE TOP(1) @message=CONVERT(xml, message_body) FROM ReceiverQueue),timeout 100;if (@@ROWCOUNT =0)begincommitbreakendselect @message--separate all tags from dataSET @PKey= @message.value ('(v/@PID)[1]','nchar(10)')SET @BKey=@message.value('(v/@BedID)[1]','nchar(10)')SET @StartTime=GETDATE()SET @HKey =@message.value('(v/@HR)[1]','nchar(10)')SET @vKey=@message.value('(/v)[1]','nvarchar(12)')SELECT @PKey,@BKey ,@StartTime,@HKey,@vKeyif ((SELECT COUNT(*) FROM dbo.Table1 WHERE pid=@PKey and bedno=@BKey)=0 )BEGINset transaction isolation level read uncommittedBEgin transaction t1-- add new recordINSERT INTO dbo.Table1 (bedno,pid,starttime,hrtype,v)VALUES (@BKey,@PKey,@StartTime,@HKey,@vKey)commit transaction t1END ELSE-- find out if current time and previous time have a difference of 10 min--upto 10 min data should go to same record--patient id same, bed name same and difference upto 10 minif (SELECT COUNT(*) FROM dbo.Table1 WHERE(DATEDIFF(mi,starttime,@StartTime) <= 10 AND DATEDIFF(mi,starttime,@StartTime) > 0 AND pid=@PKeyand bedno=@BKey))=1BEGINset transaction isolation level read uncommittedbegin transaction t2--now add v tagdeclare @l intselect @l=datalength(Table1.v) from table1 where (DATEDIFF(mi,starttime,@StartTime) <= 10 AND DATEDIFF(mi,starttime,@StartTime) > 0 AND pid=@PKeyand bedno=@BKey)DECLARE @ptr varbinary(16)SELECT @ptr = TEXTPTR(v)FROM dbo.Table1WHERE (DATEDIFF(mi,starttime,@StartTime) <= 10 AND DATEDIFF(mi,starttime,@StartTime) > 0 AND pid=@PKeyand bedno=@BKey)UPDATETEXT dbo.Table1.v @ptr @l 0 @vKeycommit transaction t2ENDELSE IF -- in else the new record need to be added(SELECT COUNT(*) FROM dbo.Table1 WHERE(DATEDIFF(mi,starttime,@StartTime) > 10 AND pid=@PKey and bedno=@BKey))>1BEGINset transaction isolation level read uncommittedbegin transaction t3INSERT INTO dbo.Table1 (bedno,pid,starttime,hrtype,v)VALUES (@BKey,@PKey,@StartTime,@HKey,@vKey)commit transaction t3ENDENDThanks |
|