我有獲取ID,如果它存在或返回當前的日期從表返回日期的功能,如果不是:更新場
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;
下一個查詢從表中返回正確的日期:
select dbo.closedate(4170)
select dbo.closedate(id) from issues where id = 4170
而接下來的代碼更新記錄正確的(從表中的值):
DECLARE @d AS datetime
select @d = dbo.closedate(4170)
UPDATE issues SET created_on = @d WHERE issues.id = 4170
,但我得到當前的日期在現場如果我更新記錄:
UPDATE issues
SET created_on = dbo.CloseDate(id)
WHERE issues.id = 4170
它看起來像ID參數不傳遞給函數。
是的,我都試過了。沒關係 – demas 2011-12-15 08:36:10