2012-07-30 12 views
10

Sto cercando di ottenere la data corrente in una variabile all'interno di una stored procedure SQL utilizzando i seguenti comandiutente GETDATE() per mettere la data corrente nella variabile SQL

DECLARE @LastChangeDate as date 
SET @LastChangeDate = SELECT GETDATE() 

questo mi dà il seguente errore: " Sintassi non corretta vicino a "SELEZIONA" "

Questa è la prima stored procedure che abbia mai scritto, quindi non ho familiarità con il funzionamento delle variabili all'interno di SQL.

+0

Qual è il dialetto SQL che si sta utilizzando? –

+0

Suppongo, è t-sql. – heximal

+1

7 domande, ** no ** risposte accettate? – egrunin

risposta

22

È don' t bisogno del SELECT

DECLARE @LastChangeDate as date 
SET @LastChangeDate = GetDate() 
+0

IMPOSTA "AT" exportedDate = '20160427' SET "AT" date1 = GETDATE(), "AT" exportedDate - 7 Month È possibile eseguire anche sth in questo modo? –

+0

SET "AT" date1 = DATEADD (mese, 1, "AT" exportedDate) appena scoperto provando –

2
DECLARE @LastChangeDate as date 
SET @LastChangeDate = GETDATE() 
1
SELECT @LastChangeDate = GETDATE() 
7

Basta usare GetDate() non Select GetDate()

DECLARE @LastChangeDate as date 
SET @LastChangeDate = GETDATE() 

ma se è SQL Server, è anche possibile inizializzare in stesso passo, come dichiarazione ...

DECLARE @LastChangeDate date = getDate() 
+1

L'inizializzazione nello stesso passo della dichiarazione è 2008+? – msmucker0527

+0

L'ho capito, una soluzione piuttosto semplice. Basta sbarazzarsi dell'istruzione SELECT :) Grazie ragazzi! – NealR

1

Per questo è anche possibile utilizzare CURRENT_TIMESTAMP.

Secondo BOLCURRENT_TIMESTAMP è il ANSI SQL euivalent a GETDATE()

DECLARE @LastChangeDate AS DATE; 
SET @LastChangeDate = CURRENT_TIMESTAMP; 
Problemi correlati