关于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 时失败。
关于sql条件语句where id in (@参数)执行报错问题(转换成数据类型 int 时失败)解决完成,有什么问题,可以一起在技术社区(http://bbs.25yi.com)讨论。
作者: 网站设计@ 企业网站管理系统
原载: 25亿企业网站管理系统
相关文档:
select *
from (
select soft.NETMODEL,
soft.softname,
soft.softid ......
SQL Select Into语句
The SELECT INTO Statement
SELECT INTO 语句
The SELECT INTO statement is most often used to create backup copies of tables or for archiving records.
SELECT INTO语句常用来给数据表建立备份或是历史档案。
Syntax
语法
SELECT column_name(s) INTO newtable [IN externaldatabase] ......
1.删除表
select 'drop table ' || table_name || ' purge;'
from user_tables
where length(table_name) <= 4;
删除带约束的表
Drop table 表名 cascade constraints;
2.查询view&同义词
--说明:可以根据视图的text_length ......
1。企业管理器
打开你建有外键的表--右击表--设计表--在上方点开’管理约束‘--将级连删除和级连更新的沟打上就可以了
2。查询分析器
alter table sc
add
constraint forei ......
本文来自:http://hi.baidu.com/darkroot/blog/item/7b74be2cf06d76e78b139903.html
declare @tbName nvarchar(500)
declare @ct int
declare @csql nvarchar(500)
declare &n ......