Pergunta

Good morning Stack overflow.

At the top of one of my stored procedures I have 2 variables for my dates I use. @StartDate is for the start of the current day and @EndDate is for the current time of the day.

DECLARE @StartDate DATE
DECLARE @EndDate DATETIME

SET @StartDate = GETDATE()
SET @EndDate = GETDATE()

PRINT @StartDate
PRINT @EndDate

I was wondering if it is possible to set both @StartDate and @EndDate to GETDATE() in the same line?

Thank you

Foi útil?

Solução

I'll put you out of your misery: No, you can't. My guess here is that either you want both variables to be exactly the same (eliminating any possibility of two calls to GETDATE() returning different values, however small), or you just want fewer function calls. SQL Server doesn't support syntax like

SET @StartDate = @EndDate = GETDATE()

That's even leaving aside the implicit narrowing from DATETIME to DATE (althoguh that's not really an issue). Best you can do is

SET @StartDate = GETDATE()
SET @EndDate = @StartDate

Unless you just want to save a line in which case you can comma separate the @Variable = GETDATE() calls and only use one SELECT.

EDIT

Forgot to add this example - could reduce to one line thus:

SELECT @EndDate = GETDATE(), @StartDate = @EndDate

Outras dicas

SET can only assign one variable at a time, but SELECT multiple.

so that's what you want:

SELECT @StartDate = GETDATE(), @EndDate = GETDATE()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top