不工作我有扳機上插入這樣的:的SQL Server:功能觸發
ALTER TRIGGER [dbo].[trTblNameSetRefNo]
ON [dbo].[TblName]
AFTER INSERT
AS BEGIN
UPDATE TblName
SET RefNumber = dbo.GetNextRefNo(i.SomeField)
FROM TblName
INNER JOIN inserted i on i.ID = TblName.ID
END
但它不工作。當我'硬編碼'字段的RefNumber,例如:SET RefNumber = 'test'
,它工作正常。
另外,當我在觸發器外調用函數時,它會返回正確的結果。
這是我的函數必須在觸發返回值:
ALTER FUNCTION [dbo].[GetNextRefNo]
(@ValueField INT)
RETURNS NVARCHAR(250)
AS
BEGIN
DECLARE @lastId INT;
DECLARE @result NVARCHAR(25);
DECLARE @CurrentIdentifier NVARCHAR(25);
SELECT TOP 1
@lastId = CAST(Substring(RefNumber, Charindex('-', RefNumber) + 4, Len(RefNumber)) AS INT) + 1
FROM
TblName
ORDER BY
ID DESC
IF @@ROWCOUNT < 1 --if empty table , set start number
BEGIN
SET @lastId = 1000
END
SELECT @CurrentIdentifier = 'SIT'
SET @result = @CurrentIdentifier + '-' + Substring (Cast(Year(Getdate()) AS NVARCHAR), 3, 2) + '-' + Cast(@lastId AS NVARCHAR)
RETURN @result
END
任何想法我做錯了嗎?
該功能是否在觸發器外工作? – Squirrel
什麼是錯誤信息? – ydoow
是的,功能從外面工作..我想我在我的問題mantion:'也當我在觸發器外調用函數,它會返回正確的結果。 ..我沒有收到任何錯誤,我不知道如何調試觸發器或什麼? –