一:
select SX01.ID as SX01_ID,SX02.ID as SX02_ID,V.BM_ID,V.VAL
from SX01,SX02,V
where SX01.DY_ID=V.DW_ID and SX02.DY_ID=V.KW_ID and

解决方案 »

  1.   

    表1 View  DW_ID  KM_ID  BM_ID   VAL
      101    201    301    123.00
      102    202    302    500.00
      103    203    303    600.00表2 DY (对应表:base_id 是表3的主键,dy_field是表1中的字段)     BASE_id    DY_FIELD
          1           DW_ID
          2           KM_ID表3 BASE(id 是主键 ,name 是表名)     id     name
          1     sx01
          2     sx02表4 SX01 (id 主键,dy_id 是表1中的数值)       ID     DY_ID
          8001    101     
          8002    102    表5  SX02       ID    DY_ID   
          7002    202    
          7003    203     完全对应上的显示:
    第一个SQL结果为:SX01_ID   SX02_ID   BM_ID    VAL8002       7002     302     500.00否则没有对应上的显示:
    第二个SQL结果为:DW_ID      KM_ID    BM_ID   VAL
    101        201      301     123.00
    103        203      303     600.00
      

  2.   

    上面語句多了个and ,LZ試一下~~~
      

  3.   

    create table #V(DW_ID int,  KM_ID int,  BM_ID int,  VAL decimal(10,2))
    insert #V select 101,    201,    301,    123.00
    union all select 102,    202,    302,    500.00
    union all select 103,    203,    303,    600.00Create table #DY(BASE_id int identity(1,1),    DY_FIELD varchar(10))
    insert #DY select 'DW_ID'
    union all select 'KM_ID'Create table #BASE(id int identity(1,1),    name varchar(10))
    insert #BASE select 'sx01'
    union all select 'sx02'Create table #SX01([id] bigint,  DY_ID int)
    insert #SX01 select 8001,    101 
    union all select 8002,    102Create table #SX02([id] bigint,    DY_ID int)
    insert #SX02 select 7002,    202 
    union all select 7003,    203declare @s varchar(8000)
    set @s = 'select '
    select  @s = @s + 'convert(varchar,DW_ID) [' + isnull((select name + '_id' from #DY dy join #BASE b on dy.BASE_id=b.id where dy.DY_FIELD='DW_ID'),'DW_ID') 
    + '],' +  'convert(varchar,KM_ID) [' + isnull((select name + '_id' from #DY dy join #BASE b on dy.BASE_id=b.id where dy.DY_FIELD='KM_ID'),'KM_ID') 
    + '],'  + 'convert(varchar,BM_ID) [' + isnull((select name + '_id' from #DY dy join #BASE b on dy.BASE_id=b.id where dy.DY_FIELD='BM_ID'),'BM_ID') 
    + '],'  + 'convert(varchar,VAL) [VAL]' + 'from #V v where DW_ID in (select DY_ID from #SX01) and KM_ID in (select DY_ID from #SX02)'
    exec (@s)
    select * from #V v where DW_ID not in (select DY_ID from #SX01) or KM_ID not in (select DY_ID from #SX02)
    drop table #SX02
    drop table #SX01
    drop table #BASE
    drop table #DY
    drop table #V/*sx01_id     sx02_id          BM_ID          VAL                            
    ---------------------------------------------
    102         202               302             500.00DW_ID       KM_ID       BM_ID       VAL          
    ----------- ----------- ----------- ------------ 
    101         201         301         123.00
    103         203         303         600.00
    */
     
      

  4.   

    谢谢 dutguoyi(新鲜鱼排) !
    你写的我先研究一下!如果实现一定加分!太复杂了
      

  5.   

    to :dutguoyi(新鲜鱼排)第一个查询语句有问题不是你那个结果,应该是SX01_ID   SX02_ID   BM_ID    VAL8002       7002     302     500.00