省公司字公司数据

select * from [省公司字公司数据]

exec dbo.P_detete_yincangzifu_all 省公司字公司数据

column:   Level1Name---ok
column:   Level2Name---ok
column:   Level4Name---ok


update 省公司字公司数据 set Level1Name= dbo.F_Convert(Level1Name,0)
update 省公司字公司数据 set Level2Name= dbo.F_Convert(Level2Name,0)
update 省公司字公司数据 set Level4Name= dbo.F_Convert(Level4Name,0)

update 省公司字公司数据 set Level1Name= public_data.dbo.f_GB2BIG(Level1Name,0)
update 省公司字公司数据 set Level2Name= public_data.dbo.f_GB2BIG(Level2Name,0)
update 省公司字公司数据 set Level4Name= public_data.dbo.f_GB2BIG(Level4Name,0)


Create table ParentComp(
PC_ID int identity(1,1),Comp nvarchar(255),Father_ID int

)
insert ParentComp(Comp)
select Level1Name from 省公司字公司数据
where Level1Name is not null
union
select Level2Name from 省公司字公司数据
where Level2Name is not null
union
select Level4Name from 省公司字公司数据
where Level4Name is not null

alter table 省公司字公司数据 add PC_ID1 int, PC_ID2 int,PC_ID4 int

update a set a.PC_ID1=b.PC_ID
from 省公司字公司数据 a inner join ParentComp b
on a.Level1Name=b.Comp
update a set a.PC_ID2=b.PC_ID
from 省公司字公司数据 a inner join ParentComp b
on a.Level2Name=b.Comp
update a set a.PC_ID4=b.PC_ID
from 省公司字公司数据 a inner join ParentComp b
on a.Level4Name=b.Comp

select * from 省公司字公司数据
where Level1Name is not null and PC_ID1 is  null
select * from 省公司字公司数据
where Level2Name is not null and PC_ID2 is  null
select * from 省公司字公司数据
where Level4Name is not null and PC_ID4 is  null


select distinct PC_ID1 , PC_ID2
into #ps
  from 省公司字公司数据
where PC_ID1 <> PC_ID2
union --all
select distinct PC_ID2 , PC_ID4  from 省公司字公司数据
where PC_ID2 <> PC_ID4

select * into parent_sub_comp
from #ps

select b.pc_id as pc_id_parent,b.Comp as comp_parent,c.Comp as comp_sub,c.PC_ID as pc_id_sub
into 外包数据管理20101012SQL..省公司字公司数据_Error_HaveMoreParent
 from #ps a left join ParentComp b
on a.PC_ID1=b.PC_ID
left join ParentComp c
on a.PC_ID2=c.PC_ID
where PC_ID2 in (
select PC_ID2
from #ps
group by PC_ID2
having COUNT(1)>1)
order by PC_ID2

update B
set b.PC_ID1=null
 from sheet1$ a
inner join 省公司字公司数据 b
on a.pc_id_parent=b.PC_ID1 and a.pc_id_sub=b.PC_ID2
where a.sig='error'


update B
set b.PC_ID4=null
 from sheet1$ a
inner join 省公司字公司数据 b
on a.pc_id_parent=b.PC_ID2 and a.pc_id_sub=b.PC_ID4
where a.sig='error'

update B
set b.PC_ID1=PC_ID2
 from sheet1$ a
inner join 省公司字公司数据 b
on a.pc_id_parent=b.PC_ID1 and a.pc_id_sub=b.PC_ID2
where a.sig='相同'

update B
set b.PC_ID2=PC_ID4
 from sheet1$ a
inner join 省公司字公司数据 b
on a.pc_id_parent=b.PC_ID2 and a.pc_id_sub=b.PC_ID4
where a.sig='相同'

 

 

select distinct PC_ID1 , PC_ID2
into #ps
  from 省公司字公司数据
where PC_ID1 <> PC_ID2
union --all
select distinct PC_ID2 , PC_ID4  from 省公司字公司数据
where PC_ID2 <> PC_ID4

select * into parent_sub_comp
from #ps

select b.pc_id as pc_id_parent,b.Comp as comp_parent,c.Comp as comp_sub,c.PC_ID as pc_id_sub
--into 外包数据管理20101012SQL..省公司字公司数据_Error_HaveMoreParent
 from #ps a left join ParentComp b
on a.PC_ID1=b.PC_ID
left join ParentComp c
on a.PC_ID2=c.PC_ID
where PC_ID2 in (
select PC_ID2
from #ps
group by PC_ID2
having COUNT(1)>1)
order by PC_ID2
6977 The Government of the Hong Kong Special Administrative Region 圣德肋撒医院 119904
81535 ST.TERESA HOSPITAL SCANNING CENTRE Co.Ltd 圣德肋撒医院 119904
83567 HSBC Holiding plc 香港上海汇丰银行有限公司 121673
7181 HSBC Purchasing (Asia) Ltd 香港上海汇丰银行有限公司 121673
113027 中国电力投资集团公司 中国电力投资集团公司河南分公司 123004
90291 国家电网公司 中国电力投资集团公司河南分公司 123004

delete from  parent_sub_comp
where (pc_id1 =6977 and pc_id2=119904)
or (pc_id1 =83567 and pc_id2=121673)
or (pc_id1 =113027 and pc_id2=123004)


update a
set a.Father_ID=b.pc_id1
 from ParentComp a inner join parent_sub_comp b
on a.PC_ID=b.PC_ID2

select * from ParentComp


alter proc P_ParentComp
(
@Ceil int=0
)
as
begin
set nocount on ;

declare @sql nvarchar(4000)='',@count int=1,@args nvarchar(4000)='',@m int=1,@jointbl nvarchar(4000),@sqlexec nvarchar(4000)
while @count >=0
begin
set @sqlexec=@sql
set @args=@args+',t'+CONVERT(varchar,@m)+'.pc_id as pc_id'+CONVERT(varchar,@m)+',t'+CONVERT(varchar,@m)+'.comp as comp'+CONVERT(varchar,@m)

if @m=1 
set @jointbl=' (select a.PC_ID,a.comp from ParentComp a where father_id is null )t1 '
else
set @jointbl=@jointbl+' left join
(select a.PC_ID,a.comp,a.father_id from ParentComp a where father_id is not null )t'+CONVERT(varchar,@m)+'
on t'+CONVERT(varchar,@m-1)+'.PC_ID=t'+CONVERT(varchar,@m)+'.Father_ID
'
set @sql='select @count=count(1)
from
'+@jointbl+'
where '+
(case when @m=1 then '1=1 ' else
't'+CONVERT(varchar,@m)+'.Father_ID is not null
' end)
print @sql
exec sp_executesql @sql,N'@count int output',@count output
print @count
set @sql='select '+STUFF(@args,1,1,'')+'
from
'+@jointbl+'
'

set @m=@m+1
if @count =0
break;
end
exec(@sqlexec)
--select a.PC_ID as pc_id1,a.comp as comp1,
--b.pc_id as pc_id2,b.comp as comp2,
--c.pc_id as pc_id3,c.comp as comp3,
--d.pc_id as pc_id4,d.comp as comp4
--from
--(select a.PC_ID,a.comp from ParentComp a where father_id is null )a
--left join
--(select a.PC_ID,a.comp,a.Father_ID from ParentComp a where father_id is not null ) b
--on a.PC_ID=b.Father_ID
--left join
--(select a.PC_ID,a.comp,a.Father_ID from ParentComp a where father_id is not null )c
--on b.PC_ID=c.Father_ID
--left join
--(select a.PC_ID,a.comp,a.Father_ID from ParentComp a where father_id is not null )d
--on c.PC_ID=d.Father_ID
--where d.Comp is not null

end

 

你可能感兴趣的:(JOIN,sql,table,null,delete,insert)