创建将从字符串中删除数字的触发器

时间:2013-01-07 01:37:00

标签: sql-server triggers

我创建了从字符串中移除数字的函数

create function RemoveNumber(@inp nvarchar(50))
returns nvarchar(50)
as
begin
declare @out varchar(50)
if (@inp is NOT NULL)
begin
    set @out = ''
    while (@inp <> '')
    begin
        if (@inp like '[0-9]%')
            set @out = @out + substring(@in, 1, 1)
        set @inp = substring(@inp, 2, len(@in) - 1)
    end
end
return(@out)
end

然后我想在触发器中使用它

create trigger ut_RemoverName
on Worker
for insert
as
update Worker
set name = ut_RemoveNumber(name)

它没有看到这个功能。但是如何从触发器内的RemoveNumber创建循环?(字段name - nvarchar(10)

1 个答案:

答案 0 :(得分:2)

我想你需要这个

create trigger ut_RemoverName on Worker
for insert
as
UPDATE w
SET Name = dbo.RemoveNumber(i.name)
FROM Worker w JOIN inserted i ON w.Id = i.Id

SQLFiddle上的演示