我已经在整个stackoverflow中看到过这个问题,但似乎有很多针对这种情况的解决方案。据我所知,似乎我有一个独特的情况。我运行这条SQL语句在sql server中将varchar转换为INT的问题
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);
我会得到错误:消息245,级别16,状态1,行21 将varchar值“”1“转换为数据类型int时转换失败。
这个错误发生在代码段的最后一行我已经表明:
SET @returnCode = CAST(@CAReturnCode AS INT);
这是被写了一个同事和假想代码为他工作。我必须解决一些错误,但我无法调试这一个。我了解很多人会创建一个dbo.split函数?我不知道在这种情况下这个选项是否会对我有所帮助。我曾尝试将@returnCode设置为varchar,并删除@CAReturnCode上的CAST。因此,调试器将使其通过该行,但会引发其他代码的问题。我假设我是如何投射@CAReturnCode的问题?任何帮助将非常感激。
CAST是ANSI,而Convert是SQL特有的,你可以尝试使用CONVERT,因为它更加智能化,它选择转换值。 –
我给了一个镜头 SET @returnCode = CONVERT(INT,@CeteturnCode); 仍然收到相同的错误 – TaylorSmolik