loading a UDF's statements into a string variable
We have a stored procedure that uses UDFs. We need to modif开发者_运维知识库y the content of these UDFs on the fly. Yeah, I know, I know... Not my choice. This is exactly what we are tying to do:
- Read the contents of a UDF
- Modify the contents and create a new UDF
- Use the UDF in an
executesql
statement
Is there a way to get the contents of a udf into a varchar (or nvarchar) that I can then modify using string functions?
select object_definition(object_id('UDFName'))
Is there a way to get the contents of a udf into a varchar (or nvarchar) that I can then modify using string functions?
declare @str nvarchar(max)
-- get function definition to @str
select @str = object_definition(object_id('UDFName'))
-- Modify definition
set @str = replace(@str, 'CREATE FUNCTION', 'ALTER FUNCTION')
-- execute alter function
exec (@str)
精彩评论