A
A
Alex Serov2015-11-28 21:38:35
SQL
Alex Serov, 2015-11-28 21:38:35

How to get the name of a function in MS SQL to pass it to DROP FUNCTION?

There is a query that displays information about all user-defined functions:

USE master
GO
SELECT name AS function_name
  ,object_id
  ,SCHEMA_NAME(schema_id) AS schema_name
  ,type_desc
  ,create_date
  ,modify_date
FROM sys.objects
WHERE type_desc LIKE '%FUNCTION%';
GO

How to remove one of the functions?
DROP FUNCTION requires a function name, but the following request is marked as an error:
DECLARE @FuncName sysname = (
     SELECT MIN(name)
     FROM sys.objects
     WHERE type_desc LIKE '%FUNCTION%' AND SCHEMA_NAME(schema_id) = 'dbo'
)
DROP FUNCTION @FuncName

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igruschkafox, 2015-11-28
@gibsonman01

Dynamic SQL needs
DECLARE @FuncName sysname = (
SELECT MIN(name)
FROM sys.objects
WHERE type_desc LIKE '%FUNCTION%' AND SCHEMA_NAME(schema_id) = 'dbo'
)
DECLARE @SQL Varchar(200)
set @SQL ='DROP FUNCTION ' + @FuncName
EXECUTE (@sql)
Note:
If you get an error, you can try
EXECUTE IMMEDIATE (@sql)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question