关于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亿企业网站管理系统
相关文档:
SQLServer和Oracle的常用函数对比
1.绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
2.取整(大)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.取整(小)
S:select floor(-1.001) value
O:select floor(-1.001) value fr ......
select *
from (
select soft.NETMODEL,
soft.softname,
soft.softid ......
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
impo ......
2008数据库附加
/*
网上看到的整理了一下。
原文地址http://database.51cto.com/art/201003/190984.htm
在SQL Server 7.0中,微软推出了sp_attach_db和sp_attach_single_file_db系统存储过程。
它对于SQL Server数据库管理员执行下面的任务是非常方便的:
1 使用sp_attach_d ......
第一种:
SELECT
CASE
WHEN
price IS NULL THEN
'Not yet priced'
WHEN
price < 10 THEN
'Very Reasonable Title'
WHEN
price >= 10 AND
price < 20 THEN
'Coffee Table Title'
EL ......