select  b.empno,b.buildid
     from   
       
       
 (select *
from openquery (TEST, 'select     /*+ RULE */    * from  db1.ta_show 
where compcd=''11''   and corpcd=''C11''   and workdt like ''201301%''  
 
   ') ) a,(select *
from openquery (TEST, 'select     /*+ RULE */    * from  db1.tb_show
where compcd=''11''   and corpcd=''C11''    and statuscd=''D'' 
 
   ')) b
   where a.id=b.id
   这样写可以查出数据,,,如果  workdt like ''201301%''  
不要写死的,要是当月,left(CONVERT(varchar(100), GETDATE(), 112),6)  能得到201301但如果把这放进去
select  b.empno,b.buildid
     from   
       
       
 (select *
from openquery (TEST, 'select     /*+ RULE */    * from  db1.ta_show 
where compcd=''11''   and corpcd=''C11''   and workdt like ''left(CONVERT(varchar(100), GETDATE(), 112),6)%''  
 
   ') ) a,(select *
from openquery (TEST, 'select     /*+ RULE */    * from  db1.tb_show
where compcd=''11''   and corpcd=''C11''    and statuscd=''D'' 
 
   ')) b
   where a.id=b.id
又不对,得怎么写好呢?

解决方案 »

  1.   

    当月的话,lz是否可以用 datediff(yy,col,getdate()) =0 and datediff(mm,col,getdate())=0 来处理?参考
    DECLARE @i DATETIME 
    SET @i = '2013-01-01'SELECT CASE WHEN datediff(yy,@i,getdate()) =0 and datediff(mm,@i,getdate())=0 THEN '同月' ELSE '不同月' END--同月
      

  2.   

    workdt   中的值是20130101 这样的格式的
      

  3.   


    字符串的话 convert转称datetime ,如果直接就是datetime,一样用。
    DECLARE @i DATETIME 
    SET @i = '20130101'SELECT CASE WHEN datediff(yy,@i,getdate()) =0 and datediff(mm,@i,getdate())=0 THEN '同月' ELSE '不同月' END--同月
      

  4.   

     (select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.ta_show 
    where compcd=''11''   and corpcd=''C11''   and and     datediff(yy,convert(datetime,workdt),getdate()) =0  anddatediff(mm,convert(datetime,workdt),getdate())=0  
     
       ') ) a,(select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.tb_show
    where compcd=''11''   and corpcd=''C11''    and statuscd=''D'' 
     
       ')) b
       where a.id=b.id
       
    这样也不行啊
      

  5.   

    (select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.ta_show 
    where compcd=''11''   and corpcd=''C11''    and     datediff(yy,convert(datetime,workdt),getdate()) =0  and  datediff(mm,convert(datetime,workdt),getdate())=0  
     
       ') ) a,(select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.tb_show
    where compcd=''11''   and corpcd=''C11''    and statuscd=''D'' 
     
       ')) b
       where a.id=b.id
       
    这样也不行啊
      

  6.   

    知道怎么弄了,唉,因为DBLINK的是oracle,所以要这样写
    (select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.ta_show 
    where compcd=''11''   and corpcd=''C11''    and    substr(workdt,1,6) =to_char(sysdate,''yyyymm'')  
       ') ) a,(select *
    from openquery (TEST, 'select     /*+ RULE */    * from  db1.tb_show
    where compcd=''11''   and corpcd=''C11''    and statuscd=''D'' 
     
       ')) b
       where a.id=b.id