一:
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
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
解决方案 »
- 求教:如何设计一个系统中的表结构
- [求助]求教insert语句的使用问题
- 再请教 MSSQL2005 深度计算多版本bom单价
- 数据库连接问题。等高手指教
- 数据库问题,一个sql数据库原来只有一张表,现在想分为多表。主要是数据库量太大。怎么效率才会高呢?
- 怎么样选择表A中字段YpID与表B中字段YpID内容不相同的所有记录的所有字段?
- 安装 SQL SERVER2000 企业版 出错 !!!!!!
- ACCESS2000中SQL更新语句?
- 这个触发器怎么写?
- 太郁闷!!如何在一个触发器中确切知道一个正对这个表的事务已经开始和结束的那一点
- 请教一个SQLSERVER里的子查询的问题 急
- 高分求解,用sql语句能实现CRC8校验吗?
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
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
*/
你写的我先研究一下!如果实现一定加分!太复杂了