我在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);
我将收到错误消息:将 varchar 值 '"1"' 转换为数据类型 int 时,Msg 245, Level 16, State 1, Line 21 Conversion failed。
此错误发生在我显示的代码段的最后一行:
SET @returnCode = CAST(@CAReturnCode AS INT);
这是由一位同事编写的代码,据说为他工作过。我不得不解决一些错误,但我无法调试这个。我知道很多人会创建一个 dbo.split 函数?我不知道这个选项在这种情况下是否对我有帮助。我尝试将@returnCode 设置为varchar 并摆脱@CAReturnCode 上的CAST。结果,调试器将使其越过该行,但会引发其余代码的问题。我假设我如何投射@CAReturnCode 存在问题?任何帮助将非常感激。