
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FNMED_TABLETAMELIYATLGILERI]') AND xtype in (N'FN', N'IF', N'TF'))
	DROP FUNCTION [dbo].[FNMED_TABLETAMELIYATLGILERI]
GO
CREATE FUNCTION [dbo].[FNMED_TABLETAMELIYATLGILERI](@SUBEID INT, @TABLETIP NVARCHAR(15)) RETURNS @TABLETAMELIYAT TABLE (
  AMELIYATID int,
  HADISOYADI nvarchar(120) NULL,
  ACIKLAMA nvarchar(500) NULL,
  BASSAAT varchar(5),
  BITISSAAT varchar(5)
) AS BEGIN
INSERT INTO
  @TABLETAMELIYAT (
    AMELIYATID,
    HADISOYADI,
    ACIKLAMA,
    BASSAAT,
    BITISSAAT
  )
SELECT
  TOP 6 AMELIYATID,
  HADISOYADI,
  ACIKLAMA,
  CONVERT(varchar(5), SAAT, 108) AS BASSAAT,
  CONVERT(varchar(5), ORTSAAT, 108) AS BITISSSAT
FROM
  DUYURU WITH(NOLOCK)
WHERE
  AKTIF = 1
  AND KYTARIH > DATEADD(HOUR, -24, GETDATE())
ORDER BY
  CONVERT(date, KYTARIH) DESC,
  ORTSAAT ASC RETURN
END
GO

