Returns insurance name
USE [myvisionexpress]
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetPatientInsurance] Script Date: 7/24/2016 5:39:10 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_GetPatientInsurance]
(
@ContactID int,
@InsType varchar(100),
@InsSequence varchar(100)
)
RETURNS varchar(100)
AS
BEGIN
DECLARE @RetVal varchar(50)
SET @RetVal = (select top 1 i.companyname
from insurance i
left outer join patient_insurance pi on i.id = pi.insurance_company_id
where pi.contactid = @ContactID and pi.insurance_type = @InsType and pi.insurance_sequence = @InsSequence and pi.status_flag = 'Yes')
RETURN @RetVal
END
GO