^^^^^^^^^^一道难题

cefriend 2009-12-21 01:44:03

select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
(case when job.is_update=0 then pact.PactID = job.sync_filed else pact.Map_PactID=job.sync_filed end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;



表结构如下:

PAC_InfoPact 表
PactID PactCode Map_PactID
65 H411111 00650
66 H511111 00660

DataJob 表
ID sync_filed is_update is_sync
1 65 0 0
2 66 1 0

如上示例, 在 inner join 的 on 后面用 case 语法,大家用过没有,,

我的这种语句有错误,,,,,,,,,,,,,,,,在线请教大家 能帮我改下吗


...全文
161 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
huayexian 2009-12-21
  • 打赏
  • 举报
回复
学习了
dawugui 2009-12-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 cefriend 的回复:]
SQL codeselect pact.PactID, pact.PactCode,job.is_updatefrom PAC_InfoPact pactinnerjoin DataJob jobon
(casewhen job.is_update=0then pact.PactID= job.sync_filedelse pact.Map_PactID=job.sync_filedend)where job.sync_tname='PAC_InfoPact'and job.is_sync=0;


表结构如下:

PAC_InfoPact 表
PactID  PactCode  Map_PactID
65      H411111    00650
66      H511111    00660

DataJob 表
ID  sync_filed is_update  is_sync
1      65          0        0
2      66          1        0

如上示例, 在 inner join 的 on 后面用 case 语法,大家用过没有,,

我的这种语句有错误,,,,,,,,,,,,,,,,在线请教大家  能帮我改下吗



[/Quote]
用union all

select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact  , DataJob job where job.is_update=0 and pact.PactID = job.sync_filed and job.sync_tname='PAC_InfoPact' and job.is_sync = 0
union all
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact , DataJob job where job.is_update<>0 and pact.Map_PactID=job.sync_filed and job.sync_tname='PAC_InfoPact' and job.is_sync = 0

yidichaxiang 2009-12-21
  • 打赏
  • 举报
回复
mark
雄牛 2009-12-21
  • 打赏
  • 举报
回复

路过~~~~~
友情up~~~~~~~~
wholesale3151 2009-12-21
  • 打赏
  • 举报
回复
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
(case when job.is_update=0 chinese wholesalers
ugg.PactID = job.sync_filed else pact.Map_PactID=job.sync_filed end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;
cefriend 2009-12-21
  • 打赏
  • 举报
回复
好我下就看出二楼是我想要的结果,,,,,,,,,,,,

一下子结弄糊涂了,,,,,,,,,,,,没明白case的具体用处........................



smntbk 2009-12-21
  • 打赏
  • 举报
回复
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on 
pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact' and job.is_sync = 0

union all

select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
pact.Map_PactID=job.sync_filed
where job.is_update=1 and job.sync_tname='PAC_InfoPact' and job.is_sync = 0


但是sync_tname字段好像不在表中....
nianran520 2009-12-21
  • 打赏
  • 举报
回复

--表结构有问题吧
--sync_tname无效
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on
job.sync_filed = (case when job.is_update=0
then pact.PactID else pact.Map_PactID end)
where job.is_sync = 0
------------------
65 H411111 0

--小F-- 2009-12-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]
SQL codeselect
pact.PactID, pact.PactCode,job.is_updatefrom
PAC_InfoPact pactinnerjoin DataJob jobon
job.sync_filed=(casewhen job.is_update=0then pact.PactIDelse pact.Map_PactIDend)where
job.sync?-
[/Quote]

--无视我5楼的 这样试下
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact'
union all
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.Map_PactID=job.sync_filed
where job.is_update!=0 and job.sync_tname='PAC_InfoPact'
nianran520 2009-12-21
  • 打赏
  • 举报
回复
--> 测试数据:[PAC_InfoPact]
if object_id('[PAC_InfoPact]') is not null drop table [PAC_InfoPact]
create table [PAC_InfoPact]([PactID] int,[PactCode] varchar(7),[Map_PactID] varchar(5))
insert [PAC_InfoPact]
select 65,'H411111','00650' union all
select 66,'H511111','00660'
--> 测试数据:[DataJob]
if object_id('[DataJob]') is not null drop table [DataJob]
create table [DataJob]([ID] int,[sync_filed] int,[is_update] int,[is_sync] int)
insert [DataJob]
select 1,65,0,0 union all
select 2,66,1,0

select * from [DataJob]

select * from [PAC_InfoPact]


select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on
job.sync_filed = (case when job.is_update=0
then pact.PactID else pact.Map_PactID end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;
microsofttyc 2009-12-21
  • 打赏
  • 举报
回复
on后加个连接字段
qiqi860819 2009-12-21
  • 打赏
  • 举报
回复
不懂,帮顶下吧
--小F-- 2009-12-21
  • 打赏
  • 举报
回复
select
pact.PactID, pact.PactCode,job.is_update
from
PAC_InfoPact pact inner join DataJob job
on
job.sync_filed=(case when job.is_update=0 then pact.PactID else pact.Map_PactID end)
where
job.sync_tname='PAC_InfoPact' and job.is_sync = 0
快乐_石头 2009-12-21
  • 打赏
  • 举报
回复
--try
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact'
union all
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.Map_PactID=job.sync_filed
where job.is_update!=0 and job.sync_tname='PAC_InfoPact'
  • 打赏
  • 举报
回复


这个语法sql 不支持,还是使用动态sql吧

参见:

--1. 使用 EXEC 实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 varchar(10)=null,
@para3 varchar(10)=null,
@para4 varchar(10)=null
AS
SET NOCOUNT ON
DECLARE @sql varchar(8000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
IF @para1 IS NOT NULL
SET @sql=@sql+' AND col1='''+@para1+''''
IF @para2 IS NOT NULL
SET @sql=@sql+' AND col2='''+@para2+''''
IF @para3 IS NOT NULL
SET @sql=@sql+' AND col3='''+@para3+''''
IF @para4 IS NOT NULL
SET @sql=@sql+' AND col4='''+@para4+''''
EXEC(@sql)
GO


/*======================================================*/

--2. 使用 sp_executesql 实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
DECLARE @sql nvarchar(4000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
+CASE WHEN @para1 IS NULL THEN '' ELSE ' AND col1=@para1' END
+CASE WHEN @para2 IS NULL THEN '' ELSE ' AND col2=@para2' END
+CASE WHEN @para3 IS NULL THEN '' ELSE ' AND col3=@para3' END
+CASE WHEN @para4 IS NULL THEN '' ELSE ' AND col4=@para4' END
EXEC sp_executesql @sql,N'
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
',@para1,@para2,@para3,@para4
GO


/*======================================================*/

--3. 不使用动态 Transact-SQL 语句实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
SELECT * FROM tbname
WHERE (@para1 IS NULL OR col1=@para1)
AND (@para2 IS NULL OR col2=@para2)
AND (@para3 IS NULL OR col3=@para3)
AND (@para4 IS NULL OR col4=@para4)


--小F-- 2009-12-21
  • 打赏
  • 举报
回复
inner join DataJob job on 后面应该是连接条件 你直接就是case 语句?
nalnait 2009-12-21
  • 打赏
  • 举报
回复
-- try

select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
job.sync_filed=(case when job.is_update=0 then pact.PactID else pact.Map_PactID end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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