zoukankan      html  css  js  c++  java
  • 关于sql where id in 转换成数据类型 int 时失败(转)

    有执行sql条件语句where id in(@参数)的时候,如果处理不当,就会出现问题:
    如下面这个存储过程:

    alter proc Web_gettwtwgoldgameserverGoldSell
    @ID int 
    as
    declare @twgoldsellID nvarchar(1000)
    select @twgoldsellID=twgoldsellID from twgoldgameserver where ID=@ID
    set @twgoldsellID=replace(@twgoldsellID,'|',',')
    set @twgoldsellID=left(@twgoldsellID,len(@twgoldsellID)-1) 
    select * from twgoldsell where ID in (@twgoldsellID)

    我们看上去好像没有什么问题,却在执行的时候报错:

    消息 245,级别 16,状态 1,第 1 行
    在将 varchar 值 '813,1160,1219,1227,1232' 转换成数据类型 int 时失败。

    其实此条语句在执行时,
    select * from twgoldsell where ID in (@twgoldsellID)
    执行的语句是:select * from twgoldsell where ID in ('813,1160,1219,1227,1232')
    这样执行当然出错,因为@twgoldsellID是一个字符串,现在是以参数的形式传递。

    解决办法:
    select * from twgoldsell where ID in (@twgoldsellID)
    改为:
    exec('select * from twgoldsell where ID in ('+@twgoldsellID+')')

    记住:一定要加exec执行函数,不然会报如下错误:
    消息 245,级别 16,状态 1,过程 Web_gettwtwgoldgameserverGoldSell,第 8 行
    在将 varchar 值 '+@twgoldsellID+' 转换成数据类型 int 时失败。

  • 相关阅读:
    matlab 自定义函数及调用
    matlab 逻辑数组及其应用
    matlab 嵌套循环的学习
    matlab和C语言的break及continue测试
    matlab 向量法建数组(推荐)
    matlab for循环应用(阶乘及the day of year)
    matlab for循环的三种类型
    ORACLE创建用户,建表空间,授予权限
    Linux计划任务
    android开发环境搭建
  • 原文地址:https://www.cnblogs.com/mr-jia/p/3581744.html
Copyright © 2011-2022 走看看