如何将'1,2,3'转换为Int类型以便使用in语句

earthpea earthpea 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了一个权限控制的小代码,读取时报错
消息 245,级别 16,状态 1,第 1 行
在将 varchar 值 '1,2,3,4,5,6,7,8,9,10' 转换成数据类型 int 时失败。
请大家帮忙看一下
create table t_a
(a_id int,
a_name varchar(10)
)

insert into t_a(a_id ,a_name)
select 1,'功能一'
union select 2,'功能二'

create table t_b
(b_id int,
b_name varchar(20),
b_content varchar(50))

insert into t_b(b_id,b_name,b_content)
select 1,'权限一','1,2'

create table t_c
(c_name varchar(10),
c_role int)

insert into t_c(c_name,c_role)
select '测试一',1



运行下面的语句成功:

select * from t_b where a_id in (1,2)

但运行下面的就出错:
select * from t_a
where a_id in (
select b.role_content from t_c c, t_b b
where c.c_role = b.b_id
)

sql语句应该成了:
select * from t_b where a_id in ('1,2')
这种方式,但怎么才能处理正常呢?
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
earthpea
MS-SQL Server 34,876 成员 254,635 内容