我将一个大型的MS Access应用程序转换为几乎完全在SQL Server上运行,以提高性能,因为Access DB既古老又极其缓慢。因此,我正在将VBA函数重新创建为SQL函数。 SQL Server到处都给我语法错误,我试图理解为什么,这是VBA:
Function AlphaNum(prodno As String)
Dim i As Long
If Len(prodno) = 0 Then Exit Function
For i = 1 To Len(prodno)
If Mid(prodno, i, 1) Like "[0-9a-z]" Then AlphaNum = AlphaNum & Mid(prodno, i, 1)
Next i
If IsNumeric(AlphaNum) Then
While Left(AlphaNum, 1) = "0"
AlphaNum = Mid(AlphaNum, 2)
Wend
End If
End Function
我已经在SQL领域走了这么远。。我当然也不是专家,有什么想法吗?
CREATE FUNCTION dbo.[NAL_AlphaNum]
(
@prodno varchar(10)
)
RETURNS VarChar(10)
BEGIN
DECLARE @counter INT,
@AlphaNum varchar(10)
SET @counter='1'
CASE WHEN Len(@prodno) = 0 THEN EXIT
WHILE @counter < Len(@prodno)
BEGIN
CASE WHEN Mid(@prodno, i, 1) LIKE '[0-9a-z]'
THEN @AlphaNum = @AlphaNum + Mid(@prodno, i, 1)
SET @counter = @counter + 1
END
CASE WHEN IsNumeric(@AlphaNum) THEN
WHILE Left(@AlphaNum, 1) = '0'
@AlphaNum = Mid(@AlphaNum, 2)
END
RETURN @AlphaNum
END;
谢谢。