SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

CREATE PROCEDURE [dbo].[MEDWEB_YAKLASANRANDEVULAR] 
AS
BEGIN

    SELECT HKIMLIKID,
		 RTARIH,
		 CONVERT(NVARCHAR(5), RSAAT + 2, 8) AS RSAAT,
		 BOLUMADI,
		 DRADI,
		 TEL,
		 HTCKIMLIKNO AS TCKIMLIKNO,
		 RANDEVUID,
		 CASE
			WHEN RANDDURUMID IN(1, 7)
		 AND HMUAYENEISLID IS NULL
		 AND RTARIHSAAT >= GETDATE()
			THEN 1
			ELSE 0
		 END AS IPTALEDILEBILIR
    FROM VWRANDEVU
    WHERE RANDDURUMID NOT IN(5, 6)
    AND GETDATE() BETWEEN DATEADD(MINUTE, -60, RTARIHSAAT) AND DATEADD(MINUTE, -30, RTARIHSAAT)
    ORDER BY RTARIH DESC;

  RETURN 
END

