-------------------------------------------------------------
----------------------------APpointmnet Table----------------------------------
-------------------------------------------------- 

alter table "Appointment" drop column if exists "PayTypeId";
 alter table "Appointment"
 add column if not exists "PayTypeId" integer references "PayType"("PayTypeId");
--------------------------------------
ALTER TABLE "PayType"
ALTER COLUMN "PayTypeValue"  TYPE varchar(255);
-------------------------------------------------------

with T as(
 select A."AppointmentId",PT."PayTypeId" from "Appointment" A 
  join "PayType" PT on lower(A."PaymentType") = lower(PT."PayTypeValue")
) 
update "Appointment" P set "PayTypeId" = T."PayTypeId" from T where P."AppointmentId" = T."AppointmentId";

--------------------------------------------------------
-----------------------------LAbBookingHeader----------------------------------
---------------------------------------------

alter table "LabBookingHeader" drop column if exists "PayTypeId";
 alter table "LabBookingHeader"
 add column if not exists "PayTypeId" integer references "PayType"("PayTypeId");
 ---------------------------
 with T as(
 select LBH."LabBookingHeaderId",PT."PayTypeId" from "LabBookingHeader" LBH
  join "PayType" PT on lower(LBH."PaidVia") = lower(PT."PayTypeValue")
) 
update "LabBookingHeader" L set "PayTypeId" = T."PayTypeId" from T where L."LabBookingHeaderId" = T."LabBookingHeaderId";
--------------------------------------------------------------------------