Answers:
IF EXISTS (
SELECT * FROM sysobjects WHERE id = object_id(N'function_name')
AND xtype IN (N'FN', N'IF', N'TF')
)
DROP FUNCTION function_name
GO
如果要避免使用sys *表,则可以改用(示例A中的此处):
IF object_id(N'function_name', N'FN') IS NOT NULL
DROP FUNCTION function_name
GO
要捕获的主要内容是您要删除的函数类型(在最上面的sql中,FN,IF和TF表示):
if object_id('FUNCTION_NAME') is not NULL
DROP FUNCTION <name>
您也可以在sysobjects中查找名称
IF EXISTS (SELECT *
FROM sysobjects
WHERE name='<function name>' and xtype='FN'
实际上,如果函数可以是表函数,则需要使用
xtype in ('FN','TF')
IF EXISTS
(SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'functionName')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION functionName
GO
这是我的看法:
if(object_id(N'[dbo].[fn_Nth_Pos]', N'FN')) is not null
drop function [dbo].[fn_Nth_Pos];
GO
CREATE FUNCTION [dbo].[fn_Nth_Pos]
(
@find char, --char to find
@search varchar(max), --string to process
@nth int --occurrence
)
RETURNS int
AS
BEGIN
declare @pos int --position of nth occurrence
--init
set @pos = 0
while(@nth > 0)
begin
set @pos = charindex(@find,@search,@pos+1)
set @nth = @nth - 1
end
return @pos
END
GO
--EXAMPLE
declare @files table(name varchar(max));
insert into @files(name) values('abc_1_2_3_4.gif');
insert into @files(name) values('zzz_12_3_3_45.gif');
select
f.name,
dbo.fn_Nth_Pos('_', f.name, 1) as [1st],
dbo.fn_Nth_Pos('_', f.name, 2) as [2nd],
dbo.fn_Nth_Pos('_', f.name, 3) as [3rd],
dbo.fn_Nth_Pos('_', f.name, 4) as [4th]
from
@files f;
检查是否存在功能
IF EXISTS (SELECT TOP 1 1 FROM sys.objects WHERE
object_id = OBJECT_ID(N'[Schema].[function_Name]')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
DROP FUNCTION [Schema].[function_Name]
Print('function dropped => [Schema].[function_Name]')
END
GO
通过单击以下链接http://www.gurujipoint.com/2017/05/check-if-exist-for-trigger-function-and.html来检查IF是否存在存储过程,功能。