. 1 the ALTER the PROC [ the dbo ] . [ Pro_med_records_sync ] 2 the AS . 3 the BEGIN . 4 SELECT DISTINCT patient number, passport number ID number INTO #tt from [ 192.168.101.33 ] . [ Rmlis6 ] . [ The dbo ] . [ Interface view _ main requisitions table ] WHERE filing date > Cast ( Convert ( VARCHAR ( 10 ), getdate (), 120 )+ ' 00:00:00 ' AS datetime ) . 5 . 6 - receiving synchronous . 7 SELECT DISTINCT patient number, passport number ID number INTO #tt from [ 192.168.101.33 ] . [ Rmlis6 ] . [ The dbo ] . [ Interface view _ Requisition main table ] WHERE filing date > Cast ( Convert ( VARCHAR ( 10 ), getdate (), 120 ) + ' 00:00:00 ' as datetime) 8 MERGE INTO [dbo].Yy_MedRecords t 9 USING(SELECT 10 * 11 FROM 12 OPENQUERY(HIS_DB,'SELECT PATIENTID, VISITID, ZYH, BEDNO, PATNAME, BIRTHDAY, SEX, PAT_STATUS, 13 CHARGE_TYPE, NURSE_CLASS, IN_HOS_TIME, DISGNOSE, DOCTORCODE, DOCTORNAME, DEPT_CODE, DEPT_NAME, 14 WARD_CODE, WARD_NAME, PREPAYMENTS, TOTAL_COSTS, VEDLOCK, JOB, ADDRESS, NATION, COMPANY, TEL, 15 CONTACT, RELATION, RETEL FROM NEXTCARE.VIEW_PATIENTS_INFO WHERE STATUS = ''在院''')) AS ht 16 ON t.InPatNo = ht.ZYH collate Chinese_PRC_CI_AS 17 WHEN MATCHED THEN 18 UPDATE SET t.PatName = ht.PATNAME, 19 t.Sex = ht.SEX, 20 t.DiagValue = ht.DISGNOSE, 21 t.Age=CASE WHEN DATEDIFF(YEAR,ht.BIRTHDAY,GETDATE())>0 THEN CONVERT(varchar,DATEDIFF(YEAR,ht.BIRTHDAY,GETDATE()))+'岁' 22 WHEN DATEDIFF(MONTH,ht.BIRTHDAY,GETDATE())=0 23 THEN CONVERT(varchar,DATEDIFF(DAY,ht.BIRTHDAY,GETDATE()))+'天' 24 ELSE CONVERT(varchar,DATEDIFF(MONTH,ht.BIRTHDAY,GETDATE()))+' Month ' the END , 25 t.IDNum = the CASE the WHEN t.IDNumis null THEN ( SELECT the Convert ( VARCHAR ( 100 ), A. ID number passport number Chinese_PRC_CI_AS. COLLATE) from #tt AS A WHERE A. Patient Number = HT. PATIENTID) 26 is the ELSE t.IDNum the END , 27 t.InPatNo = ht.ZYH 28 29 30 the WHEN the NOT MATCHED BY TARGET THEN
Application 1.SqlServer temporary tables
The above procedure is used to store patient information synchronization hospital, patients need to get the ID number from the Inspection Requisition table LIS system, due to the large amount of data LIS test request form, if each match change, insert operations from the LIS database query, too time-consuming.
In order to solve the above problems, to optimize the above statement by Sql temporary tables. The qualified LIS application form related fields into a temporary table, then queries from the temporary table at each match operation.
Said the following about the operation of temporary tables:
1.1 global temporary tables and local temporary tables:
Global temporary table labeled by ## t, locally #t temporary table labeled by both the main difference that the temporary table expiration time, write in the local memory during the temporary table is automatically expire after the stored procedure execution, the global only fail if a temporary table in SqlServer service restart
1.2 How to create a temporary table
select * into #t from table
2.MERGE INFO statement
2.1MERGE INTO statement usage:
MERGE INTO statement table synchronization, i.e. the synchronization of the source data table to the target table, in particular for the source and destination row in the table can be matched to be modified, while the rows in the target table exists in the original table is not present insert, there is the line in the target table does not exist in the original table to delete
2.2
MERGE INTO Target_Table as T
USING Source_Table as S
on T.Id = S.Id
WHEN MATCHED THNE
Update set T.Name = S.name
WHEN NOT MATCHED BY TARGET THNE
Insert .......
WHEN NOT MATCHED BY SOURCE THEN
Delete.../Update...
3.UPDATE update the fields in a table to another table
UPDATE a SET a.DocId =b.F_Id FROM Yy_MedRecords a, dbo.Sys_User b WHERE a.DocCode=b.F_Account
Update the data in one table to another table, the methods described above
In another wording:
UPDATE A
SET A1 = B1, A2 = B2, A3 = B3
FROM A LEFT JOIN B ON A.ID = B.ID
Here you can also right join, inner join