Sanjith M
Sanjith M

Reputation: 81

How to assign a return value from a SQL Server Function to a Variable

I have function that returns a Date value. I need to assign that return value to a declared variable.

Declare @Duedate Date
Set @Duedate = SELECT dbo.TATDueDateCaluator('2019-05-10',2)
Select @Duedate

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'SELECT'.

Upvotes: 0

Views: 3369

Answers (3)

nealkernohan
nealkernohan

Reputation: 836

You can simply define it in one line:

Declare @Duedate Date = dbo.TATDueDateCaluator('2019-05-10',2)

Select Duedate

Upvotes: 0

sticky bit
sticky bit

Reputation: 37472

If the function is scalar valued you don't even need a SELECT. Just

SET @duedate = dbo.tatduedatecaluator('2019-05-10', 2);

should do.

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269863

You can assign directly in a SELECT:

Declare @Duedate Date;

SELECT @DueDate = dbo.TATDueDateCaluator('2019-05-10', 2);

Select @Duedate;

Your code doesn't work because subqueries always need their own parentheses.

Upvotes: 2

Related Questions