I have a function that gets the identifier and returns the date from the table if it exists, or returns the current date if not:
CREATE FUNCTION [dbo].[CLOSEDATE] (@ID int) RETURNS datetime AS BEGIN DECLARE @closed int; DECLARE @result datetime; SELECT @result = created_on from dbo.statuses_history WHERE journalized_id = @ID and new_status = ''; IF @result IS NULL SELECT @result = GETDATE() RETURN (DATEADD(dd, 0, DATEDIFF(dd, 0, @result))) END;
The following queries return the correct date from the table:
select dbo.closedate(4170) select dbo.closedate(id) from issues where id = 4170
And the following code will update the record correctly (values ββfrom the table):
DECLARE @d AS datetime select @d = dbo.closedate(4170) UPDATE issues SET created_on = @d WHERE issues.id = 4170
But I get the current date in the field if I update the record:
UPDATE issues SET created_on = dbo.CloseDate(id) WHERE issues.id = 4170
It seems that the ID parameter does not go to the function.
source share