string类型自己变成int类型来比较了,求指教
我用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 列。超出了最大整数值。
怎么解决?
------解决方案--------------------
试试这样行不行?
SQL code
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
------解决方案--------------------
SQL code
--原语句改为
exec('select count(*)as pagecount from Card where CardID = '+ '''' + @CardID + '''')
------解决方案--------------------
SQL code
在使用 sql 拼接的时候
对于整型 :exec('select count(*)as pagecount from Card where CardID = '+@CardID)
对于字符类型:exec('select count(*)as pagecount from Card where CardID = '+ '''' + @CardID + '''')