Can someone tell me how I can combine the two SQL statements that count the number of messages and number of unread messages? Its ineffecient two have two statements but I don't know what to search for to get the answer im looking for. Thanks in advance.
CREATE PROCEDURE dbo.GetMessages (
@username nchar(12),
@isCount bit,
@message_count int OUTPUT,
@unread_message_count int OUTPUT
) AS
IF @isCount = 1
BEGIN
SET @message_count =
(
SELECT COUNT(*)
FROM messages
WHERE usernameTo = @username
)
SET @unread_message_count =
(
SELECT COUNT(*)
FROM messages
WHERE usernameTo = @username AND message_read = 1
)
END
ELSE
BEGIN
SELECT *
FROM messages
WHERE usernameTo = @username
END