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

してみてください

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 に答える
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
于 2013-01-24T10:57:46.890 に答える