您的位置:首页 > 数据库

sql 存储过程 执行中 遇到的 问题 小结

2012-10-24 11:02 357 查看
防注入 检验关键字

CREATE FUNCTION [dbo].[FilterWhere]
(
@isOnlyWhere bit, --0表示含 select 主语句, 1表示仅where条件的过滤
@strWhere nvarchar(4000)--where 条件
)
RETURNS nvarchar(4000)
AS
BEGIN
-- Declare the return variable here
--'and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join'--原版
declare @keywords nvarchar(500)
set @keywords='exec|insert|delete|update|master|truncate|declare|sp_executesql|drop|table'
if(@isOnlyWhere<>0)set @keywords=@keywords+'|select|from|join'
declare @myid int,@tempcount int
declare @tempkeyword nvarchar(30)
select @tempcount=count(*)from Fun_Split_SqlKeyWord(@keywords,'|')
set @myid=1

declare @nohas bit
set @nohas = 1

while (@myid-1<@tempcount)
begin
select @tempkeyword=mykeyword from Fun_Split_SqlKeyWord(@keywords,'|') where myid=@myid
--是否有关键字
if charindex(@tempkeyword,@strWhere) > 0
set @nohas = @nohas & 0
else
set @nohas = @nohas & 1

set @strWhere=replace(@strWhere,@tempkeyword,'')
set @myid=@myid+1
end
-- Return the result of the function
--RETURN @strWhere
if @nohas = 0
return dbo.FilterWhere(@isOnlyWhere,@strWhere)
--else
return @strWhere

END

过程中出现 in 时,引号的匹配时拼接的情况 下拉打印语句 用 print @str Execute sp_executesql @str 直接执行即可

过程 中 一个' 必须写成'' 才可显示正确

productid in (303776)

也可改为

CHARINDEX(ProductId,@ProductId)>0

语法

  CHARINDEX ( expression1 , expression2 [ , start_location ] )

  参数

  expression1

  一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。

  expression2

  一个表达式,通常是一个用于搜索指定序列的列。expression2属于字符串数据类型分类。

  start_location

  在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2的起始位置开始搜索。

  返回类型

  int

  注释

  如果 expression1 或 expression2之一属于 Unicode 数据类型(nvarchar 或 nchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。

  如果 expression1 或 expression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 expression1 和 expression2都为 NULL 时返回 NULL 值。

  如果在 expression2 内没有找到 expression1,则 CHARINDEX 返回 0。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: