0

我已经编写了一个 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'.
4

3 回答 3

0

函数 isumeric 返回一个 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
于 2013-01-24T10:57:46.890 回答
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 
于 2013-01-24T10:58:01.357 回答
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

值得注意的是,我没有对此进行测试,只是修复了显示中的错误。

于 2013-01-24T10:56:46.647 回答