我编写了一个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'.
答案 0 :(得分:0)
这里试试这个:
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)) = 1
BEGIN
IF((LEN(@RAWDATA) - LEN(REPLACE(@TEMP2,'/','')))>1)
BEGIN
SET @RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
SET @RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
SET @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)) = 1
BEGIN
SET @EMPNUM = @RAWDATA
RETURN @EMPNUM
END
ELSE
IF((SUBSTRING(@RAWDATA,1,1)='C') AND ISNUMERIC(SUBSTRING(@RAWDATA,2,1)) = 1)
BEGIN
SET @EMPNUM = SUBSTRING(@RAWDATA,2,LEN(@RAWDATA))
RETURN @EMPNUM
END
END
END
END
END
值得注意的是我没有对此进行测试,只是修复了显示的错误。
答案 1 :(得分:0)
函数isnumeric返回一个int
你忘了在@RAWDATA之前设置
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))= 1)
BEGIN
IF((LEN(@RAWDATA) - LEN(REPLACE(@TEMP2,'/','')))>1)
BEGIN
Set @RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
Set @RAWDATA=SUBSTRING(@RAWDATA,CHARINDEX('/',@RAWDATA)+1,LEN(@RAWDATA))
Set @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))=1)
BEGIN
SET @EMPNUM = @RAWDATA
RETURN @EMPNUM
END
ELSE
IF((SUBSTRING(@RAWDATA,1,1)='C') AND (ISNUMERIC(SUBSTRING(@RAWDATA,2,1))=1))
BEGIN SET @EMPNUM = SUBSTRING(@RAWDATA,2,LEN(@RAWDATA))
RETURN @EMPNUM
END
END
END
END
END
return null
end
答案 2 :(得分:0)
请尝试
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)) = 1
BEGIN
IF( ( Len(@RAWDATA) - Len(Replace(@TEMP2, '/', '')) ) > 1 )
BEGIN
SET @RAWDATA=Substring(@RAWDATA, Charindex('/', @RAWDATA
)
+ 1,
Len(
@RAWDATA)
)
SET @RAWDATA=Substring(@RAWDATA, Charindex('/', @RAWDATA
)
+ 1,
Len(
@RAWDATA)
)
SET @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)) = 1
BEGIN
SET @EMPNUM = @RAWDATA
RETURN @EMPNUM
END
ELSE IF ( ( Substring(@RAWDATA, 1, 1) = 'C' )
AND ( Isnumeric(Substring(@RAWDATA, 2, 1)) =
1
) )
BEGIN
SET @EMPNUM = Substring(@RAWDATA, 2, Len(
@RAWDATA)
)
RETURN @EMPNUM
END
END
END
END
END
RETURN @EMPNUM
END