我已经编写了一个 SQL 函数来从原始数据字符串中提取所需的数据,但是我在不同的地方遇到了错误并且无法恢复它们,请帮助我:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[Empnum] (@RAWDATA NVARCHAR(300))
RETURNS VARCHAR(30)
AS
BEGIN
DECLARE @TEMP1 NVARCHAR(300), @EMPNUM NVARCHAR(10), @TEMP2 NVARCHAR(300), @TEMP3 NVARCHAR(300)
SET @TEMP3 = 'Disabled'
SET @TEMP2 = @RAWDATA
IF( CHARINDEX(@TEMP3,@RAWDATA,0) = 0)
BEGIN
IF( ISNUMERIC(SUBSTRING(@RAWDATA,1,1))
BEGIN
IF((LEN(@RAWDATA) - LEN(REPLACE(@TEMP2,'/','')))>1)
BEGIN
@RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
@RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
@RAWDATA=SUBSTRING(@RAWDATA,1,CHARINDEX('/',@RAWDATA)-1)
IF( CHARINDEX('*C',@RAWDATA) <> 0 OR
CHARINDEX('CV',@RAWDATA) <> 0 OR
CHARINDEX('AV',@RAWDATA) <> 0 OR
CHARINDEX('LV',@RAWDATA)<> 0 )
BEGIN
SET @EMPNUM = ''
RETURN @EMPNUM
END
ELSE
BEGIN
IF(ISNUMERIC(SUBSTRING(@RAWDATA,1,1))
BEGIN
SET @EMPNUM = @RAWDATA
RETURN @EMPNUM
END
ELSE
IF((SUBSTRING(@RAWDATA,1,1)='C') AND (ISNUMERIC(SUBSTRING(@RAWDATA,2,1))))
BEGIN
SET @EMPNUM = SUBSTRING(@RAWDATA,2,LEN(@RAWDATA))
RETURN @EMPNUM
END
END
END
END
END
END
我面临的错误是
Msg 156, Level 15, State 1, Procedure Empnum, Line 11
Incorrect syntax near the keyword 'BEGIN'.
Msg 102, Level 15, State 1, Procedure Empnum, Line 15
Incorrect syntax near '@RAWDATA'.
Msg 156, Level 15, State 1, Procedure Empnum, Line 29
Incorrect syntax near the keyword 'BEGIN'.
Msg 156, Level 15, State 1, Procedure Empnum, Line 33
Incorrect syntax near the keyword 'ELSE'.
Msg 4145, Level 15, State 1, Procedure Empnum, Line 34
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Msg 156, Level 15, State 1, Procedure Empnum, Line 43
Incorrect syntax near the keyword 'END'.