1

I've created function which removes numbers from string

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

Then I wanted to use it inside the trigger

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

It doesn't see the function. But how to create loop from RemoveNumber inside the trigger?(Field name - nvarchar(10))

4

1 に答える 1

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 のデモ

于 2013-01-07T08:33:32.080 に答える