能否在from 之后加 case when 语句?

nevermorewish nevermorewish 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
实现如下需求
如果table1的col1字段 值为 1,从test1表中取数据
如果table1的col1字段 值为 2,从test2表中取数据
create  table test1
(
col int
);
insert test1 (col) values(10);
create table test2
(
col int
);
insert test2 (col) values(20);

create table table1
(
col1 int,
col2 int,
col3 int,
col int
);
insert table1 (col1) values(1);

--如果table1的col1字段 值为 1,从test1表中取数据
--如果table1的col1字段 值为 2,从test2表中取数据
declare @tempid int
select @tempid = col1
from table1
-- 下面代码无法使用
-- select * from (case when @tempid = 1 then test1 else test2 end)
...全文
评论 4
收藏 已收藏
分享
返回顶部
nevermorewish
MS-SQL Server 34,876 成员 254,635 内容