0

我正在用 SQL 编写一个函数,我们可以用它来验证给定表中的名字/中间名/姓氏。

目前,我正在处理我将认为无效并从输入中删除的字符的 ascii 代码列表。

我的计划是创建一个表,其中包含我认为无效的那些字符的字符代码,并编写一个光标来替换当前输入记录中的每个无效字符。

我应该通过整个 ascii 表来工作,还是有人见过类似的工作,我可以从中构建?

4

2 回答 2

2

使用 SQL 中的代码扫描数据以查找无效字符是一种非常缓慢的方法,而且不太可能让您满意。

我认为大多数人会在数据库之外进行此验证。

如果您必须在数据库中执行此操作,请编写一个使用数据库固有语言(Oracle PL/SQL、MSSQL tsql)的触发器来检查字符串,只需将有效字符列表编码到脚本中。

当一个名字带有口音或其他有趣字符的人出现时会发生什么?

于 2009-11-23T19:55:38.323 回答
1

这几乎就是我们所做的

declare @currentCharacter char(1)
declare @alphanumericString VARCHAR(250)
declare @inputStringLength int 
declare @positionIndex int

    --init variables
    select @positionIndex = 1
    select @alphanumericString = ''

    --get the string length
    select @inputStringLength = LEN(@inputString)

    --loop through the set
    while @positionIndex <= @inputStringLength
    begin
        --get each character 
        select @currentCharacter = substring(@inputString,@positionIndex,1)

        --make sure its between 0-9, A-Z, or a-z
        if (ascii(@currentCharacter) > 31 and ascii(@currentCharacter) < 126)

            set @alphanumericString = @alphanumericString + @currentCharacter

        --increament counter
        set @positionIndex = @positionIndex + 1
    end

    return @alphanumericString
end

当然,您希望对数据输入执行此操作,而不是对整个表执行此操作,因为这将花费很长时间。

于 2009-11-23T19:57:31.377 回答