I'm trying to get the current date into a variable inside a SQL stored procedure using the following commands
DECLARE @LastChangeDate as date
SET @LastChangeDate = SELECT GETDATE()
This gives me the following error: "Incorrect Syntax near 'SELECT'"
This is the first stored procedure I've ever written, so I'm unfamiliar with how variables work inside SQL.
You can also use CURRENT_TIMESTAMP
for this.
According to BOL CURRENT_TIMESTAMP
is the ANSI SQL
euivalent to GETDATE()
DECLARE @LastChangeDate AS DATE;
SET @LastChangeDate = CURRENT_TIMESTAMP;
Just use GetDate()
not Select GetDate()
DECLARE @LastChangeDate as date
SET @LastChangeDate = GETDATE()
but if it's SQL Server, you can also initialize in same step as declaration...
DECLARE @LastChangeDate date = getDate()
DECLARE @LastChangeDate as date
SET @LastChangeDate = GETDATE()
SELECT @LastChangeDate = GETDATE()
Source: Stackoverflow.com