我用textbox文本框,输入11位数字(超过int的范围了)
前台代码一路接收下来都是用的string类型
然后测试存储过程发现:
ALTER    PROCEDURE [sp_SelFindCardcount] 
@CardID nvarchar(50)
AS
set nocount on
if(@CardID='' )--查询全部
begin
 select  count(*)as pagecount  from Card
end
if(@CardID<>'' )--根据密保卡号
begin
exec('select  count(*)as pagecount  from Card where CardID = '+@CardID)
end
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
数据库一共2条记录54884485864 和  46426486131
execute sp_SelFindCardcount 54884485864   --发现一条记录(54884485864)是数据库有的值
execute sp_SelFindCardcount 54884485865   --发现0条记录(54884485865)是数据库没有的值
execute sp_SelFindCardcount 5488448586   --将 numeric 转换为数据类型 numeric 时发生算术溢出错误。
execute sp_SelFindCardcount 548844858    --nvarchar 值 '46426486131' 的转换溢出了 int 列。超出了最大整数值。
execute sp_SelFindCardcount 46426486131  ----发现一条记录(46426486131)是数据库有的值
execute sp_SelFindCardcount 4642648613   --将 numeric 转换为数据类型 numeric 时发生算术溢出错误。
execute sp_SelFindCardcount 464264861    --nvarchar 值 '46426486131' 的转换溢出了 int 列。超出了最大整数值。怎么解决?

解决方案 »

  1.   

    如果这个11位的数据是有效的数据,那LZ就需要修改sp_SelFindCardcount 里面的相关部分,替换int为bigint类型如果是无效数据,只需在前台应用程序上,判断一下即可。
      

  2.   

    试试这样行不行?ALTER PROCEDURE [sp_SelFindCardcount] 
    @CardID NVARCHAR(50)
    AS 
        SET nocount ON
        IF ( @CardID = '' )--查询全部
            BEGIN
                SELECT  COUNT(*) AS pagecount FROM Card
            END
        IF ( @CardID <> '' )--根据密保卡号
            BEGIN
                EXEC('select count(*) as pagecount from Card where ltrim(CardID) = '''+@CardID+'''')
            END
      

  3.   


    --原语句改为
    exec('select count(*)as pagecount from Card where CardID = '+ '''' + @CardID + '''')
      

  4.   

    在使用 sql 拼接的时候
    对于整型 :exec('select count(*)as pagecount from Card where CardID = '+@CardID)
    对于字符类型:exec('select count(*)as pagecount from Card where CardID = '+ '''' + @CardID + '''')