The OP does not indicate which client app they're using to execute the SQL statements. Probably SSMS, since that's the usual MS SQL Server tool? Or maybe Azure Data Studio?
In my case, I've been trying to execute such multi-statement batches in DBeaver for MS SQL Server T-SQL. As noted here, you can (sometimes) make this work by surrounding the statements with BEGIN and END. For example, for the simple query below, you can execute the following using Ctrl+Enter or Ctrl+\ anywhere in the BEGIN-END block. It will open two tabs since there are two SELECT statements.
BEGIN
DECLARE @StartDate datetime='1/1/2023 12:00:00 AM',
@EndDate datetime='12/31/2023 12:00:00 AM';
SELECT @StartDate as [Start Date];
SELECT @EndDate as [End Date];
END
For more complex queries, it can be necessary to position the cursor on the BEGIN statement and then use Ctrl+Enter or Ctrl+\. (Or, alternatively, highlight the entire BEGIN-END block and press Alt+X.) In particular, I've found this to be necessary when the SQL statement(s) contain with cte1 (...)... Common Table Expressions.
I've also been running into more complex batches of SQL statements that execute successfully in SSMS, but that DBeaver rejects because it complains that there is a syntax error. I have not found a solution to that problem.
Bottom line with respect to DBeaver: it seems it simply won't execute some MS Sql Server T-Sql multi-statement batches that SSMS and Azure Data Studio execute correctly.
SET @STRDT = '2017-01-01'SELECTand execute only thatSELECT- then of course, the@STRDTis never declared. Variables in T-SQL are not persisted - you must declare them before each use