Why do I get a syntax error on the following SQL statements:
DECLARE @Count90Day int;
SET @Count90Day = SELECT COUNT(*) FROM Employee WHERE DateAdd(day,30,StartDate) BETWEEN
DATEADD(day,-10,GETDATE()) AND DATEADD(day,10,GETDATE()) AND Active ='Y'
I am trying to assign the number of rows returned from my Select statement to the variable @Count90Day.
You need parentheses around the subquery:
DECLARE @Count90Day int;
SET @Count90Day = (SELECT COUNT(*)
FROM Employee
WHERE DateAdd(day,30,StartDate) BETWEEN DATEADD(day,-10,GETDATE()) AND
DATEADD(day,10,GETDATE()) AND
Active ='Y'
);
You can also write this without the set
as:
DECLARE @Count90Day int;
SELECT @Count90Day = COUNT(*)
FROM Employee
WHERE DateAdd(day,30,StartDate) BETWEEN DATEADD(day,-10,GETDATE()) AND DATEADD(day,10,GETDATE()) AND
Active ='Y';
You can assign it within the SELECT
, like so:
DECLARE @Count90Day int;
SELECT @Count90Day = COUNT(*)
FROM Employee
WHERE DateAdd(day,30,StartDate) BETWEEN
DATEADD(day,-10,GETDATE()) AND DATEADD(day,10,GETDATE()) AND Active ='Y'
You don't assign SQL variables in a SELECT
like that, you can do:
SELECT @Count90Day = COUNT(*)
FROM Employee WHERE DateAdd(day,30,StartDate) BETWEEN
DATEADD(day,-10,GETDATE()) AND DATEADD(day,10,GETDATE()) AND Active ='Y'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With