幾次遇到關於兩個無關的表合併問題,因人太懶了,我都不知道如解決的了,
最近又遇到了(原因是同類產品有不同的數據結構表---想想那個設計者我就想日),沒法必須要解決了,終於下定決心了來解決了
如下:
********************************************************************************************************************
不周表結構合併問題表一
不周表結構合併問題A     B      C       D     FED1        FED2             FED3        
不周表結構合併問題147    110    141.06    147.88    NULL    1998-12-01 00:00:00.000    122.15
不周表結構合併問題表二
不周表結構合併問題E       F       G      H      I      J      K    FED1        FED2             FED3
不周表結構合併問題146.81    114.38    182.88    143.06    152.31    47.67    63.36    NULL    1999-01-01 00:00:00.000    126.69
不周表結構合併問題合並表:
不周表結構合併問題A    B    C    D        E    F    G    H    I    J    K    FED1    FED2                FED3
不周表結構合併問題147    110    141.06    147.88        0      0    0    0    0    0    0    NULL    1998-12-01 00:00:00.000    122.15
不周表結構合併問題0            0        0                0            146.81    114.38    182.88    143.06    152.31    47.67    63.36    NULL    1999-01-01 00:00:00.000    126.69
不周表結構合併問題*@Function:從表一表二得到合併表(注:表一表二的結構未知)
不周表結構合併問題*@Author: 何利民
不周表結構合併問題*@Create Date:2005-02-24
不周表結構合併問題*@Parameters:@tbName1表一@tbName2表二
不周表結構合併問題*********************************************************************************************************************/
不周表結構合併問題
CREATE PROCEDURE  [dbo].[MergeTable] 
不周表結構合併問題(
不周表結構合併問題@tbName1 
nvarchar(50),
不周表結構合併問題@tbName2 
nvarchar(50)
不周表結構合併問題)
不周表結構合併問題
AS
不周表結構合併問題
--將要合併的兩個表分並存在臨時表##tmp與##tmp1
不周表結構合併問題
declare @sqlStr nvarchar(4000)
不周表結構合併問題
set @sqlStr='select * into ##tmp from '+@tbName1+'; select * into ##tmp1 from '+@tbName2
不周表結構合併問題
exec(@sqlStr)
不周表結構合併問題
declare @tb1 nvarchar(50)
不周表結構合併問題
declare @tb2 nvarchar(50)
不周表結構合併問題
set @tb1='##tmp'
不周表結構合併問題
set @tb2='##tmp1'
不周表結構合併問題
不周表結構合併問題
--在表一中增加表二中多余的結構
不周表結構合併問題
set @sqlStr=''
不周表結構合併問題
declare @fed nvarchar(50)
不周表結構合併問題
Declare newcursor Cursor local forward_only static read_only type_warning
不周表結構合併問題
for select [name] from tempdb.dbo.syscolumns where id=(select id from tempdb.dbo.sysobjects where id=object_id(N'[tempdb].[dbo].['+@tb2+']'))
不周表結構合併問題
open newcursor
不周表結構合併問題
fetch next from newcursor into @fed
不周表結構合併問題
while(@@fetch_status=0)
不周表結構合併問題
begin
不周表結構合併問題    
if(not exists(select [name] from tempdb.dbo.syscolumns where id=(select id from tempdb.dbo.sysobjects where id=object_id(N'[tempdb].[dbo].['+@tb1+']')) and name=@fed))
不周表結構合併問題    
begin
不周表結構合併問題        
set @sqlStr=@sqlStr+'Alter table '+@tb1+'  '
不周表結構合併問題        
set @sqlStr=@sqlStr+'add ['+@fed+'] nvarchar(10);'
不周表結構合併問題    
end
不周表結構合併問題    
fetch next from newcursor into @fed
不周表結構合併問題
end
不周表結構合併問題
exec(@sqlStr)
不周表結構合併問題
close newcursor
不周表結構合併問題
deallocate newcursor
不周表結構合併問題
不周表結構合併問題
--在表二中增加表一中多余的結構
不周表結構合併問題
set @sqlStr=''
不周表結構合併問題
Declare newcursor Cursor local forward_only static read_only type_warning
不周表結構合併問題
for select [name] from tempdb.dbo.syscolumns where id=(select id from tempdb.dbo.sysobjects where id=object_id(N'[tempdb].[dbo].['+@tb1+']'))
不周表結構合併問題
open newcursor
不周表結構合併問題
fetch next from newcursor into @fed
不周表結構合併問題
while(@@fetch_status=0)
不周表結構合併問題
begin
不周表結構合併問題    
if(not exists(select [name] from tempdb.dbo.syscolumns where id=(select id from tempdb.dbo.sysobjects where id=object_id(N'[tempdb].[dbo].['+@tb2+']')) and name=@fed))
不周表結構合併問題    
begin
不周表結構合併問題        
set @sqlStr=@sqlStr+'Alter table '+@tb2+'  '
不周表結構合併問題        
set @sqlStr=@sqlStr+'add ['+@fed+'] nvarchar(10);'
不周表結構合併問題    
end
不周表結構合併問題    
fetch next from newcursor into @fed
不周表結構合併問題
end
不周表結構合併問題
exec(@sqlStr)
不周表結構合併問題
close newcursor
不周表結構合併問題
不周表結構合併問題
--對兩表進行合併結果存入臨時表##resultTable
不周表結構合併問題
declare @fedStr nvarchar(4000)
不周表結構合併問題
set @fedStr=''
不周表結構合併問題
select @fedStr=@fedStr+'['+name+'],'  from tempdb.dbo.syscolumns where id=(select id from tempdb.dbo.sysobjects where id=object_id(N'[tempdb].[dbo].['+@tb1+']'))
不周表結構合併問題
if(right(@fedStr,1)=',')
不周表結構合併問題    
set @fedStr=left(@fedStr,len(@fedStr)-1)
不周表結構合併問題
set @sqlStr='select *  into  ##resultTable  from  (select '+@fedStr+'  from ##tmp union all select '+@fedStr+'  from  ##tmp1) a'
不周表結構合併問題
exec(@sqlStr)
不周表結構合併問題
不周表結構合併問題
drop table ##tmp
不周表結構合併問題
drop table ##tmp1
不周表結構合併問題
GO
不周表結構合併問題

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
  • 2022-12-23
  • 2021-08-01
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-06
  • 2021-12-13
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案