本文介绍了我收到此错误“将varchar值'A1'转换为数据类型int时转换失败。”当我尝试使用存储过程选择varchar valure时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 ALTER PROCEDURE [dbo]。[InsertAlbum_sp] ( @ Member_Id varchar ( 10 ), @ albumTitle varchar ( 50 )) AS BEGIN - 添加SET NOCOUNT ON以防止来自 $的额外结果集b $ b - 干扰SELECT语句。 SET NOCOUNT ON ; 声明 @ newId varchar ( 10 ) 声明 @ length int if ( select MAX(album_Id)来自 album_tb)<> ' null' 开始 set @length =(选择 len(max(album_Id))来自 album_tb) set @newId =(选择 SUBSTRING(max(album_Id), 1 , @ length )+ 1 来自 album_tb) set @ newId = ' A' + RIGHT(强制转换( @ newId as varchar ( 10 )), 1 ) end else 开始 set @ newId = ' A1' end insert into album_tb(album_Id,Member_Id,albumTitle)值( @ newId , @ Member_Id , @ albumTitle ) 选择 @ newId END 解决方案 如果要存储整数数据,请首先使用整数数据类型,而不是 varchar 。看起来使用代表数据的字符串代替数据的趋势本身就成了初学者的真正诅咒。 -SA 没有惊喜,消息很清楚,你试图转换''A1''这是一个无效的表示形式整数(除非它是十六进制表示,在这种情况下你必须明确地将它转换成这样),到 int 。 ALTER PROCEDURE [dbo].[InsertAlbum_sp](@Member_Id varchar(10),@albumTitle varchar(50))ASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;declare @newId varchar(10) declare @length intif(select MAX(album_Id) from album_tb)<>'null'beginset @length=(select len(max(album_Id)) from album_tb)set @newId=(select SUBSTRING(max(album_Id),1,@length)+1 from album_tb)set @newId='A'+RIGHT(cast(@newId as varchar(10)),1)endelsebeginset @newId='A1'endinsert into album_tb (album_Id,Member_Id,albumTitle) values (@newId,@Member_Id,@albumTitle)select @newIdEND 解决方案 If you want to store integer data, use integer data types, not varchar, in first place. It looks like the trend to work with strings representing data instead of data itself became a real curse of the beginners these days.—SANo surprises, the message is clear, you are trying to convert ''A1'' that is a NOT valid representation of an integer number (unless it is an hexadecimal representation, in that case you have to explicitely convert it as such), to an int. 这篇关于我收到此错误“将varchar值'A1'转换为数据类型int时转换失败。”当我尝试使用存储过程选择varchar valure时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 17:47