最近为了优化数据库执行效率,把原有代码中的用得最频繁的数据库操作改成了参数化查询的方式,但是。有个带有in的sql语句,就让人郁闷了。数据库中的数据老是没更新,检查了半天,最后还是把DEBUG_MODE编译参数打开,查看出错信息,才定位到错误。
'sqlStr = "update tbCommand set isHandled = 1 where id IN (?);"
执行时出错,将varchar转换成bigint时出错。只好改成
sqlStr = "update tbCommand set isHandled = 1 where id=?;"
然后在一个循环中逐个更新。
网上也没找到这个问题的较好的解决方法。
msdn上有一篇HOW TO: Implement Helper Class to use a SqlParameter with a list of values in a IN clause,竟是用了临时表。不知道有没人遇到过类似问题,最后是怎么解决的?
'sqlStr = "update tbCommand set isHandled = 1 where id IN (?);"
执行时出错,将varchar转换成bigint时出错。只好改成
sqlStr = "update tbCommand set isHandled = 1 where id=?;"
然后在一个循环中逐个更新。
网上也没找到这个问题的较好的解决方法。
msdn上有一篇HOW TO: Implement Helper Class to use a SqlParameter with a list of values in a IN clause,竟是用了临时表。不知道有没人遇到过类似问题,最后是怎么解决的?
解决方案 »
- VBA 标准输出流Wscrpt.write
- 求一个listitem.tag存放文件路径的例子
- 高手请进,关于VB中构造c语言的struct中的union,谢谢
- 怎样在ComboBox中显示日文?
- 怎样将刚刚添加到MSFlexGrid中的数据处于被选中的状态
- VB编程中,如何根据已知的数据源名称取得ODBC中对应的数据库名称
- 如何使msflexgrid 控件中的内容左对起、居中 或右对起?????
- DataGrid控件数据源不支持Microsoft Jet.OLEDB.4.0数据引擎问题
- 我编了4年程序,可是我在这里的提问没有一个人的回答让我满意!!!!
- 简单问题,求答。
- Controls.Add添加OptionButton控件问题?
- 读online《MOSS案例实战开发》有感
Set param = cmdCommandUpdate.CreateParameter("IDS", adVarChar, adParamInput, 1000)
cmdCommandUpdate.Parameters.Append param是字符串的。我也不知道这种情况下该用什么数据类型的
以前直接写in (" & strID & ");"
strid="4,5,6"
也是可以的。
in()语句里面包含的内容在数据库里不是简单的字符型,其中的逗号含义用自己赋值进去的变量不能取代.
我一般用的就是传一个字符串进去,然后在数据库存储过程里分割.
或者用临时表类型:declare @t as table(col varchar(10))
看起来差不多吧。
declare @code as varchar(10)
declare @t table(col varchar(10))while(charindex('#',@codes)<>0) --@codes 是传进来的参数
begin
insert @t(col) values (substring(@codes,1,charindex('#',@codes)-1))
set @codes = stuff(@codes,1,charindex('#',@codes),'')
end
if len(@codes)>=8 and len(@code)<=10
insert @t(col) values (@codes) update tableB set field2='Yes' where field1 in(select col from @t)
@code 一般长度为8-10 ,故条件视你自己的情况而定
如果你in子句要处理比如4,5,6三个值,就要将sql语句解析成三个参数:
sqlStr = "update tbCommand set isHandled = 1 where id IN (?,?,?);"还有关于参数的赋值处理,你可以参考,希望对你有帮助:http://blog.csdn.net/vbman2003/archive/2010/12/06/6057503.aspx
不过,事先并不知道 IN 后面的参数个数,所以。。
----------------------------------------
这种情况经常用到的,实际上就是拼接sql语句...