2

SQL サーバーで 1 つのクエリを実行していますが、「データ型 varchar を数値に変換中にエラーが発生しました」というエラーが表示されます。データベースにクエリを実行しているクエリを以下で見つけてください。

SELECT   Limit = (Case isnull(PR009000.COVERAGE ,null)
     When isnull('UM',null) then        
        (Case
            When 'SPLIT' = 'CSL' then  ( select UMLimitTypeDes from CASeleUMLimitType where UMCoverageTypeCd =( 'SPLIT' )  
                 and UMLimitTypeCd=(Select top 1 USAMNT02 From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA' )
                 and StateCd= (select top 1 RATESTE  from PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1) )
            When 'SPLIT' = 'SL' then (select UMLimitTypeDes from CASeleUMLimitType where UMCoverageTypeCd=( 'SPLIT' )  
                 and UMLimitTypeCd=(Select top 1 USAMNT02 From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
                 and StateCd=(select top 1 RATESTE  from PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1))
            When 'SPLIT' = 'BI' then (select UMLimitTypeDes   from CASeleUMLimitType where UMCoverageTypeCd=( 'SPLIT' )  
                 and UMLimitTypeCd =cast((Select top 1 USAMNT03 From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA' ) AS varchar(100))
                 and StateCd =cast((select top 1 RATESTE  from PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1) AS varchar(100)))
            When 'SPLIT' = 'N' or 'SPLIT' = 'SPLIT' then ( select top 1 isnull(UMLimitTypeDes,null) from CASeleUMLimitType where UMCoverageTypeCd=( 'SPLIT' )  
                 and UMLimitTypeCd=(Select top 1 isnull(USAMNT03,null) From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0' and MCO = '05' and LOC= '00' and INSLINE = 'CA' )
                 and StateCd=(select  top 1 isnull(RATESTE,null)  from PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1) )
            When 'SPLIT' = 'PD' then (

                select isnull(UMLimitTypeDes,null) from CASeleUMLimitType where UMCoverageTypeCd=( 'SPLIT' )  
                 and UMLimitTypeCd=(Select top 1 isnull(USAMNT04,null) From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
                 and StateCd=(select top 1 isnull(RATESTE,null)  from PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1))  

        End )
     When isnull('UN',null) then        
        (Case
            When 'SPLIT' = 'CSL' then
                (Select isnull(USAMNT02,null) From PR009000 Where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and coverage='UN')
            When 'SPLIT' = 'BI' then
                (Select isnull(USAMNT03,null) From PR009000 Where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and INSLINE = 'CA' and coverage='UN')
            When 'SPLIT' = 'N' then
                (Select isnull(USAMNT03,null) From PR009000 Where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and INSLINE = 'CA' and coverage='UN')    


        End )

 When 'DOC-MP' then
     (Select USCDE14 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
 When 'DOC-UM' then
     (Select USIND06 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
 When 'DOC-UN' then
     (Select USIND05 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
 When 'DOCUMP' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='DOCUMP')
 When 'DOCUNP' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='DOCUNP')
 When 'DTHBEN' then
     (Select USAMNT03 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='DTHBEN')
 When 'HIRE' then
     (Select USAMNT05 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
 When 'HIREUM' then
     (Select USIND09 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
 When 'HIREUN' then
     (Select USIND12 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
     When 'LIAB' then
     (Select USAMNT05 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )
     When 'MEDPAY' then
     (Select USCDE02 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )         
     When 'NONUM' then
     (Select USCDE08 From PR006000 where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'  )         
     When 'SOUND' then
     (Select USAMNT03 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='SOUND')         
     When 'TRLCMP' then
     (Select USAMNT03 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='TRLCMP')         
     When 'TRLCOL' then
     (Select USAMNT03 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='TRLCOL')         
     When 'TRLSP' then
     (Select USAMNT03 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='TRLSP')         
     When 'UMPD' then
     (Select USAMNT01 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='UMPD')         
     When 'REG-UM' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='REG-UM')     
     When 'REG-UN' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='REG-UN')     
     When 'REG-L' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='REG-L')     
     When 'REG-MP' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='REG-MP')     
     When 'REG-PIP' then
     (Select USAMNT04 From PR009000 where SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1 and insline = 'CA' and coverage='REG-PIP')          

    Else
    ( Select '-1' )
    End )
     FROM PR009000 WHERE SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1

ここで PR009000 は、そのようなデータを持つテーブルです。

select *  FROM PR009000 WHERE SYMBOL='CPP' AND POLNUM=0001148 AND MODULE=00 AND LOC=00 AND MCO=05 AND PCO=05 AND UNTRECNUM=1

これはクエリで使用される別のテーブルです

Select * From PR006000 Where Symbol= 'CPP' and POLNUM = '0001148' and MODULE = '0 ' and MCO = '05' and LOC= '00' and INSLINE = 'CA'

どこに問題があり、これに対する解決策を教えてください。

4

1 に答える 1

5

私たちはあなたのテーブル定義を知りません。あなたのクエリを解析しようとはしません。しかし、問題はほぼ確実に、概念的には次のようなものです。

CASE
   WHEN Condition 1 THEN <decimal value>
   WHEN Condition 2 THEN <string value>
END

問題は、CASE の結果が明確に定義された単一のデータ型でなければならないことです。すべてTHENの句は、この単一の型の値を返すか、それに変換する必要があります。

型が混在している場合、SQL Server はさまざまな優先規則を使用して型を選択します。この場合、数値型が選択されており、文字列を数値に変換できません。

これを解決する通常の方法は次のとおりです。

CASE
   WHEN Condition 1 THEN CONVERT(varchar(200),<decimal value>)
   WHEN Condition 2 THEN <string value>
END

そのため、SQL Server はすべてを文字列として保持します。

于 2012-09-11T07:50:39.063 に答える