简体   繁体   中英

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'.

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.

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

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

should do.

You can simply define it in one line:

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

Select Duedate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM