SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

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

CREATE PROCEDURE dbo.MEDWEB_GECMISRANDEVULAR @HKIMLIKID int, @SUBEID int
AS
BEGIN
  SELECT RTARIH, CONVERT(nvarchar(5),RSAAT+2,8) AS RSAAT, BOLUMADI, DRADI, TEL, RACIKLAMA, RANDEVUID, CASE WHEN RANDDURUMID IN (1,7) AND HMUAYENEISLID IS NULL AND RTARIHSAAT >= GETDATE() THEN 1 ELSE 0 END AS IPTALEDILEBILIR FROM VWRANDEVU  WHERE SUBEID = @SUBEID AND HKIMLIKID=@HKIMLIKID AND RANDDURUMID NOT IN (5,6)
  ORDER BY RANDEVUID DESC
  RETURN 
END
GO
