别人说这样的语句不可以

jdhlowforever 2010-11-03 05:20:42
string strsql="select * from cmr_productcolor where id in(select pro_color from cmr_T_ProductBase where pro_gdscode='"+gds+"')";


这里读出来的 pro_color 是 '1,2,3' 是字符型

数据库调试出现
在将 varchar 值 '1,2,3' 转换成数据类型 int 时失败。

我又不想分开来读
我怎样才能把 pro_color 转化成可以执行的呢?

谢谢!

...全文
267 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
Thinking-7 2010-11-04
  • 打赏
  • 举报
回复
写个split分割函数,
不是几分钟的事情吗?
以后类似问题都可以解决了,
给split函数定义一个分割符号,
你先按什么符号分割就按什么分割,多爽,
有必要再这纠结吗?
jdhlowforever 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 chen_ya_ping 的回复:]

pro_color那 你直接把这个字段设置为int型
[/Quote]

不行呢。其它地方还要用到这个。。所以字段类型不能变

看样子是没有转化的方法了
chen_ya_ping 2010-11-04
  • 打赏
  • 举报
回复
pro_color那 你直接把这个字段设置为int型
__还是少年 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 whb147 的回复:]
很明显不可以,类型不匹配

你的id是int
pro_color 是varchar
[/Quote]

应该是这个问题吧
beyond_me21 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kkbac 的回复:]

可以这样写.
SQL code
select * from cmr_productcolor where charindex(',' + ltrim(id) + ',', (select ',' + pro_color + ',' from cmr_T_ProductBase where pro_gdscode='"+gds+"')) > 0";
[/Quote]
顶这个
hookyzlr 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 karascanvas 的回复:]
引用 11 楼 hookyzlr 的回复:

用cast转换应该是可以的。你检查下pro_color的值是不是1,2,3之类的,每个数字之后是不是有空格?
如果你定义成varchar类型,就有可能会出现这个问题


"1,2,3" 这样的字符串能用cast转成整数才怪了。
[/Quote]
额,那是我理解错了,如果是"1,2,3"这种肯定不行。我以为是'1','2','3'这种类型的呢。汗了
种草德鲁伊 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hookyzlr 的回复:]

用cast转换应该是可以的。你检查下pro_color的值是不是1,2,3之类的,每个数字之后是不是有空格?
如果你定义成varchar类型,就有可能会出现这个问题
[/Quote]

"1,2,3" 这样的字符串能用cast转成整数才怪了。
hookyzlr 2010-11-04
  • 打赏
  • 举报
回复
用cast转换应该是可以的。你检查下pro_color的值是不是1,2,3之类的,每个数字之后是不是有空格?
如果你定义成varchar类型,就有可能会出现这个问题
种草德鲁伊 2010-11-04
  • 打赏
  • 举报
回复
搜索关键字 sql exec sp_executesql
jdhlowforever 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 claire448943695 的回复:]

strsql="select * from cmr_productcolor where id in(select CAST(pro_color as int) from cmr_T_ProductBase where pro_gdscode='"+gds+"')";

用CAST强制将varchar转成int
[/Quote]

这个方法行不通,我测试了。没用,如果有用的话,那是最简单的方法了。。
rabbit5 2010-11-04
  • 打赏
  • 举报
回复
只是个想法,用动态sql语句做拼接
jdhlowforever 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 mayanly 的回复:]

引用 14 楼 beyond_me21 的回复:
引用 7 楼 kkbac 的回复:

可以这样写.
SQL code
select * from cmr_productcolor where charindex(',' + ltrim(id) + ',', (select ',' + pro_color + ',' from cmr_T_ProductBase where pro_g……
[/Quote]

这样是可以,,但我想问问有没有能够转化的东东。。嘎嘎
直接转换 varchar 转 int
mayanly 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 beyond_me21 的回复:]
引用 7 楼 kkbac 的回复:

可以这样写.
SQL code
select * from cmr_productcolor where charindex(',' + ltrim(id) + ',', (select ',' + pro_color + ',' from cmr_T_ProductBase where pro_gdscode='"+gds+"')) > 0";

……
[/Quote]这个可以试一下
hubeiliuyaping 2010-11-04
  • 打赏
  • 举报
回复
就是来看看
Claire448943695 2010-11-03
  • 打赏
  • 举报
回复
strsql="select * from cmr_productcolor where id in(select CAST(pro_color as int) from cmr_T_ProductBase where pro_gdscode='"+gds+"')";

用CAST强制将varchar转成int
kkbac 2010-11-03
  • 打赏
  • 举报
回复
可以这样写.
select 	* from 	cmr_productcolor where charindex(',' + ltrim(id) + ',', (select ',' + pro_color + ',' from cmr_T_ProductBase where pro_gdscode='"+gds+"')) > 0";

jdhlowforever 2010-11-03
  • 打赏
  • 举报
回复
总的来说。。看样子还是要分开写。。还是不能这样写。........
flyerwing 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 juge001 的回复:]
C# code
string strsql="select * from cmr_productcolor where id in(select pro_color from cmr_T_ProductBase where pro_gdscode="+gds+")";
去掉两个单引号
[/Quote]
linq搞那个也是很好能的了,等到你做报表就知道了.
baihualin1983 2010-11-03
  • 打赏
  • 举报
回复
用存储过程吧,先把“pro_color ”读出来转换成整形然后再执行select * from cmr_productcolor where id in..
whb147 2010-11-03
  • 打赏
  • 举报
回复
写个自定义函数切分'1,2,3' 为
1
2
3
即可
加载更多回复(2)

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧