22,209
社区成员
发帖
与我相关
我的任务
分享
select a.表名,a.字段名,b.公司名 from
(select distinct 表名,字段名 from os) a
left join (select * from os where 公司名='aaa') b
on a.表名=b.表名 and a.字段名=b.字段名
create table os(表名 varchar(50),字段名 varchar(50),公司名 varchar(50))
insert into os select 'tlb1','fld1','aaa'
insert into os select 'tlb1','fld2','bbb'
insert into os select 'tlb1','fld1',null
insert into os select 'tlb1','fld2',null
declare @company varchar(20)
set @company='ccc'
select
a.表名,a.字段名,b.公司名
from
(select distinct 表名,字段名 from os) a
left join
(select * from os where 公司名=@company) b
on
a.字段名=b.字段名
and
a.表名=b.表名
/*
表名 字段名 公司名
----------- ------------- -----------
tlb1 fld1 NULL
tlb1 fld2 NULL
*/
drop table os
create table os(表名 varchar(50),字段名 varchar(50),公司名 varchar(50))
insert into os select 'tlb1','fld1','aaa'
insert into os select 'tlb1','fld2','bbb'
insert into os select 'tlb1','fld1',null
insert into os select 'tlb1','fld2',null
create proc wsp
@c varchar(50)=null
as
if(isnull(@c,'')='')
select * from os where isnull(公司名,'')=isnull(@c,'')
else
select * from os where 公司名=@c
union all
select a.* from os a,os b where b.公司名=@c and a.字段名<>b.字段名 and isnull(a.公司名,'')=''
exec wsp
exec wsp 'aaa'
select
a.表名,a.字段名,b.公司名
from
(select distinct 表名,字段名 from 表) a
left join
(select * from 表 where 公司名=@company) b
on
a.字段名=b.字段名
and
a.公司名=b.公司名