SQL Server 2008 Updating VarChar Column with a UDF

2019-07-18 13:46发布

问题:

I have a scalar function that takes a two variables @input1 @input2 and it returns the value of @input1 and @input2 (actual thing is more complex but this distills the idea).

I want to update all rows in a table column using this function, passing the value 'abc ' for @input1 and using the column name in @input2, so my update statement would look something like:

update mytable set mycolumn = (select dbo.myfunc( 'abc ' , mycolumn ) )
-- prepend the literal 'abc ' to every row for column mycolumn

But this is of course not allowed.

I'm trying to perform some mass string handling on a couple of columns based on some string rules. All ideas appreciated.

Thanks.

回答1:

UPDATE mytable
    SET mycolumn = dbo.myfunc('abc', mycolumn)