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     00660DataJob 表
ID  sync_filed is_update  is_sync
1      65          0         0
2      66          1         0如上示例, 在 inner join 的 on 后面用 case 语法,大家用过没有,,我的这种语句有错误,,,,,,,,,,,,,,,,在线请教大家   能帮我改下吗

解决方案 »

  1.   

    -- tryselect 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;
      

  2.   

    inner join DataJob job on 后面应该是连接条件  你直接就是case 语句?
      

  3.   

    这个语法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)
      

  4.   

    --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'
      

  5.   

    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
      

  6.   

    --> 测试数据:[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,0select * 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;
      

  7.   


    --无视我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'
      

  8.   


    --表结构有问题吧
    --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
      

  9.   

    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 = 0union allselect 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字段好像不在表中....
      

  10.   

    好我下就看出二楼是我想要的结果,,,,,,,,,,,,一下子结弄糊涂了,,,,,,,,,,,,没明白case的具体用处........................
      

  11.   

    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;
      

  12.   

    用union allselect 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