if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RecCount]') and xtype in (N'FN', N'IF', N'TF')) drop function [dbo].[RecCount] GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATE FUNCTION dbo.RecCount (@acmastid varchar(40),@dt datetime,@locid varchar(40),@secid varchar(40)) RETURNS numeric(15) AS BEGIN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ declare @retCnt numeric(15) select @retCnt = count(*) from acmast left join billmast on acmast.billmastid = billmast.billmastid where acparentid =@acmastid and acmast.AcOpenDt<= @dt and acmast.locid =@locid and acmast.r=0 and acmast.t =0 and (billmast.dt is null or billmast.dt> @dt) return @retCnt END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO