0

私はこの質問をstackoverflow全体で見てきましたが、状況に合わせた解決策がたくさんあるようです. 私が知る限り、私は独特の状況にあるようです。このSQLステートメントを実行しています

use IST_CA_2_Batch_Conversion
GO
--T-SQL script to populate the Match type column
declare @MatchType varchar(16),
@PK varchar(500),
@CAReturnCode VARCHAR(255), 
@CAErrorCodes VARCHAR(255)

declare cursor1 cursor fast_forward for
select 
["Ref#"],
["Return Code"],
["Error Codes"]
from CACodes2MatchType

open cursor1
fetch next from cursor1 into @PK,@CAReturnCode,@CAErrorCodes

while @@fetch_status = 0
begin

set @MatchType = dbo.GetMatchType(@CAReturnCode,@CAErrorCodes)

update CACodes2MatchType
set [Match Type] = @MatchType
where ["Ref#"] = @PK

fetch next from cursor1 into @PK,@CAReturnCode,@CAErrorCodes
end
close cursor1
deallocate cursor1

で失敗します

set @MatchType = dbo.GetMatchType(@CAReturnCode,@CAErrorCodes)

GetMatchType 関数の最初のコードは次のとおりです。

-- Batch submitted through debugger:    
 SQLQuery14.sql|6|0|C:\Users\b01642a\AppData\Local\Temp\~vs1C8E.sql
 CREATE FUNCTION [dbo].[GetMatchType](@CAReturnCode VARCHAR(255), @CAErrorCodes    
 VARCHAR(255))
 RETURNS VARCHAR(16)
 BEGIN 
  DECLARE @MatchType VARCHAR(16);
  DECLARE @errorCodes TABLE(Pos INT, Code CHAR(2));
  DECLARE @country INT;   -- 1 is US, 2 is Canada
  DECLARE @numMinorChanges INT;
  DECLARE @numMajorChanges INT;
  DECLARE @numSingleCodes INT;
  DECLARE @returnCode INT;

  DECLARE @verified VARCHAR(16);
  DECLARE @goodFull VARCHAR(16);
  DECLARE @tentativeFull VARCHAR(16);
  DECLARE @poorFull VARCHAR(16);
  DECLARE @multipleMatch VARCHAR(16);
  DECLARE @unmatched VARCHAR(16);

  SET @verified = 'Verified';
  SET @goodFull = 'Good Full';
  SET @tentativeFull = 'Tentative Full';
  SET @poorFull = 'Poor Full';
  SET @multipleMatch = 'Multiple Match';
  SET @unmatched = 'Unmatched';

  SET @returnCode = CAST(@CAReturnCode AS INT);

次のエラーが表示されます: Msg 245, Level 16, State 1, Line 21 Conversion failed when conversion failed when conversion the varchar value '"1"' to data type int.

このエラーは、私が示したコード セグメントの最後の行で発生します。

SET @returnCode = CAST(@CAReturnCode AS INT);

これは同僚によって書かれたコードで、おそらく彼のために働いていたと思われます。いくつかのエラーをトラブルシューティングする必要がありましたが、これをデバッグできません。多くの人が dbo.split 関数を作成することを理解していますか? このオプションがこのシナリオで役立つかどうかはわかりません。@returnCode を varchar に設定し、@CAReturnCode の CAST を削除しようとしました。その結果、デバッガーはその行を通過しますが、残りのコードで問題が発生します。@CAReturnCode のキャスト方法に問題があると思いますか? どんな助けでも大歓迎です。

4

1 に答える 1

2

問題は、@CAReturnCode に数字以外の文字が含まれていることです。

-- Msg 245, Level 16, State 1, Line 21 Conversion failed when converting the varchar value '"1"' to data type int.

外側の一重引用符はエラー メッセージの書式設定ですが、内側の二重引用符は @CAReturnCode 値にあります。したがって、ここでの解決策は、変換前に変数に数字のみが含まれるようにすることです。二重引用符が唯一の可能性である場合は、次のような簡単で汚い修正を行うことができます。

set @returnCode = cast(replace(@CAReturnCode, '"', '') as int)

さらに可能性がある場合は、複数の REPLACE 呼び出しを実行するか、指定したすべての文字を一度に削除するより優れた文字トリミング関数を構築できます。

于 2013-04-01T14:30:39.307 に答える