SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_SetSNILS]') and xtype = N'P')
drop procedure [dbo].[sp_SetSNILS]
GO
create procedure [dbo].[sp_SetSNILS]
 @PatientID uniqueidentifier = null
as
BEGIN
 if @PatientID is not null
 begin
  update p
  set p.SNILS = f.Snils
  from [AKUZ].T_PATIENT p, [AKUZ].T_FR_PATIENT f
  where p.FirstName = f.FirstName
   and p.LastName = f.LastName
   and p.FatherName = f.FatherName
   and p.Birthday = f.Birthday
   and p.PatientID = @PatientID
 end
 else
 begin
  update p
  set p.SNILS = f.Snils
  from [AKUZ].T_PATIENT p, [AKUZ].T_FR_PATIENT f
  where p.FirstName = f.FirstName
   and p.LastName = f.LastName
   and p.FatherName = f.FatherName
   and p.Birthday = f.Birthday
 end
 
 update [AKUZ].T_FR_RECORD set DateEnd = null where DateEnd < '1900-01-01'
 update [AKUZ].T_FR_RECORD set DateNsuEnd = null where DateNsuEnd < '1900-01-01'
END
GO