Reputation: 10709
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.
Upvotes: 24
Views: 147851
Reputation: 146597
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()
Upvotes: 13
Reputation: 638
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;
Upvotes: 1
Reputation: 247860
You don't need the SELECT
DECLARE @LastChangeDate as date
SET @LastChangeDate = GetDate()
Upvotes: 42
Reputation: 32740
DECLARE @LastChangeDate as date
SET @LastChangeDate = GETDATE()
Upvotes: 3