数据库自定义函数

WBOY
发布: 2016-06-07 17:37:35
原创
1183 人浏览过

这个为通用过滤关键字的函数,若有其他关键字未添加可以参考语法加入。 USE [Db] GO /****** Object: UserDefinedFunction [dbo].[f_FilterString] Script Date: 12/09/2013 17:03:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE func

 

这个为通用过滤关键字的函数,若有其他关键字未添加可以参考语法加入。

USE [Db]
GO

/****** Object: UserDefinedFunction [dbo].[f_FilterString] Script Date: 12/09/2013 17:03:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE function [dbo].[f_FilterString] --通用函数。过滤关键字
(@SqlString nvarchar(1000))--参数为需要过滤的参数
returns nvarchar(1000)
as
begin
declare @FiString nvarchar(20)
declare @count int


set @FiString='select'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='delete'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end


set @FiString='drop'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='truncate'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='--'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end


set @FiString='update'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='insert into'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='create'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

set @FiString='alter'
set @count= CHARINDEX(@FiString,@SqlString)

if(@count>0)
begin
set @SqlString=replace(@SqlString,@FiString,'')
end

return @SqlString

end


GO

 

这个为通用分割函数:第一个参数为分割前的字符串,第二个参数为分割字符

USE [Db]
GO
/****** Object: UserDefinedFunction [dbo].[f_splitstr] Script Date: 09/30/2013 21:26:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[f_splitstr](@SourceSql varchar(8000),@StrSeprate varchar(100))
returns @temp table(F1 varchar(100))
as
begin
declare @ch as varchar(100)
set @SourceSql=@SourceSql+@StrSeprate
while(@SourceSql'')
begin
set @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,1)-1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,1,charindex(@StrSeprate,@SourceSql,1),'')
end
return
end

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!