I asked an earlier version of this question yesterday, but Stack Exchange closed it because it did not meet their strict requirements for a valid question. I'm trying again with updated information. Several people made helpful comments before the question was closed, which enabled me to make some progress.
Here is the latest version of my stored procedure.
USE [IncomeExpense]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP PROCEDURE IF EXISTS dbo.ExpensesByMonth
GO
CREATE PROCEDURE dbo.ExpensesByMonth
@first varchar(10),
@last varchar(10) = Null
AS
BEGIN
SET NOCOUNT ON;
DECLARE @dfirst DATE,
@dlast DATE;
PRINT CONCAT( 'LENGTH Of FIRST IS ', LEN(@first) );
if LEN(@first)=10
BEGIN
SET @dfirst = CONVERT(DATE,@first,120);
END;
else if LEN(@first)=7
BEGIN
SET @dfirst = CONVERT(DATE,@first+'-01',120);
END;
else
BEGIN
RAISERROR('The start date format is invalid',16,1) WITH NOWAIT;
RETURN;
END;
if @last IS null
BEGIN
SET @dlast = EOMONTH(@dfirst)
END
else if len(@last) = 7
BEGIN
SET @dlast = EOMONTH(@last+'-01')
END;
else
BEGIN
RAISERROR('The end date format is invalid',16,1) WITH NOWAIT;
RETURN;
END;
SELECT Category,
format( sum(Amount), 'C', 'en-US' ),
format( sum(
CASE WHEN Regular='Y' THEN Amount ELSE 0 END
), 'C', 'en-US' )
FROM IncomeExpense.dbo.IncomeExpense
WHERE Date BETWEEN @dfirst and @dlast
GROUP BY Category
END;
My original problem was that SQL Server said the DROP PROCEDURE IF EXISTS statement was invalid. I found that I was using SQL SSMS 2022 with Server 2014, which doesn't support that statement. I installed the latest versions of both SSMS and SQL Server. Then I tried to run the procedure again, and got the same error. I found that SSMS was still using SQL Server 2014.
I found instructions that told me that to change the version of SQL Server SSMS uses, I should reconnect to SQL Server and specify the new version in the connection dialog. I did that, then I tried to run the procedure, and I got the same error. SELECT @@VERSION says that SSMS is still using SQL Server 2014.
The SQL Server installer told me that it was updating SQL Server 21 and installing SQL Server 22. It didn't say anything about SQL Server 2014, although I know I was running 2014 before. Now the connection dialog shows that I've selected 22 instead of 21, and SSMS is still using 2014. How can I fix this?