SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_PrivateMessages_UnreadCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_PrivateMessages_UnreadCount]
GO


create procedure [dbo].[cs_PrivateMessages_UnreadCount] (
	@SettingsID	int,
	@UserID		int
)
AS
BEGIN
	-- Ensure data integrity
	DELETE
		cs_PrivateMessages
	WHERE
		ThreadID not in (SELECT ThreadID from cs_Threads)

	SELECT ThreadID  FROM cs_PrivateMessages WHERE  SettingsID = @SettingsID AND UserID = @UserID
	exec dbo.cs_forums_threads_ThreadsRead 0, @UserID, @SettingsID

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT EXECUTE on [dbo].[cs_PrivateMessages_UnreadCount]  to public
GO