
 IF NOT EXISTS(SELECT* FROM sys.columns c WHERE c.[object_id] = OBJECT_ID(N'AKUZ.T_AMBULANCE_CARD') AND name = 'State')
 	ALTER TABLE [AKUZ].[T_AMBULANCE_CARD] ADD [State] uniqueidentifier NULL
    GO
 
 update ac
 set ac.State = (select top 1 State 
 				from [AKUZ].T_REGISTRATION with(nolock)
 				where LpuMain = ac.CardLpu and Status = 1 and RegistrationID = [dbo].[fn_GetRegistration](ac.Patient, null, null))
 from AKUZ.T_AMBULANCE_CARD ac