IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MEDWEB_HASTASIFREHATIRLAT]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE MEDWEB_HASTASIFREHATIRLAT
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[MEDWEB_HASTASIFREHATIRLAT]') AND xtype in (N'FN', N'IF', N'TF'))
DROP FUNCTION [dbo].[MEDWEB_HASTASIFREHATIRLAT]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[MEDWEB_HASTASIFREHATIRLAT] (@HCEP_KIMLIKNO nvarchar(20), @HEMAIL nvarchar(40))
RETURNS @retHASTASIFRE TABLE 
(
	HCEP nvarchar(20) NULL,
	HEMAIL nvarchar(40) NULL,
	HWEBSIFRE nvarchar(10) NULL,
	SONUC int NOT NULL
) 
AS
BEGIN
    DECLARE 
      @KAYITSAYI int

	IF (@HCEP_KIMLIKNO IS NOT NULL)
	BEGIN
	    --SET @KAYITSAYI = 0
	    --SELECT @KAYITSAYI = COUNT(*) FROM HKIMLIK WHERE HWEBKULLAN = 1 AND HWEBAKTIF = 1 AND (HCEP = @HCEP_KIMLIKNO OR HTCKIMLIKNO = @HCEP_KIMLIKNO)
	    --IF @KAYITSAYI = 1
 			INSERT INTO @retHASTASIFRE (HCEP,HEMAIL, HWEBSIFRE, SONUC)
			SELECT HCEP,HEMAIL,HWEBSIFRE, 0 AS SONUC FROM HKIMLIK WHERE HWEBKULLAN = 1 AND HWEBAKTIF = 1 AND (HCEP = @HCEP_KIMLIKNO OR HTCKIMLIKNO = @HCEP_KIMLIKNO)
	END
	
	ELSE IF (@HEMAIL IS NOT NULL)
	BEGIN
	    --SET @KAYITSAYI = 0
	    --SELECT @KAYITSAYI = COUNT(*) FROM HKIMLIK WHERE HWEBKULLAN = 1 AND HWEBAKTIF = 1 AND HEMAIL = @HEMAIL	 
	    --IF @KAYITSAYI = 1
			INSERT INTO @retHASTASIFRE (HCEP,HEMAIL, HWEBSIFRE, SONUC)
			SELECT HCEP,HEMAIL,HWEBSIFRE, 0 AS SONUC FROM HKIMLIK WHERE HWEBKULLAN = 1 AND HWEBAKTIF = 1 AND HEMAIL = @HEMAIL		 
	END
RETURN

END

GO


