1
我正在使用sql server 2000,並且需要從我的服務器中的所有數據庫 中讀取所有數據,因爲那樣我將處理一些數據,我該如何使查詢? 我試過這段代碼,但並沒有按我想要的方式工作。讀取我服務器中所有數據庫中的所有數據
ALTER PROCEDURE TMP_INGRESO_MASIVO_DAT_CLIENTES AS
declare @Fila as int
declare @Script_Dinamico as varchar(8000)
declare @Script_Dinamico1 as varchar(8000)
declare @Nro_Registros as int
Select Distinct cod_empresa,Nombre_Base Into #Tmp_Bases1 From Estadistica_Cobranza.Dbo.Sis_Conexion_pagos Where Cod_Estado = 1 and Proc_Automaticos= 1--capturo nombre BD
Select Distinct Identity(Int,1,1) As Fila, cod_empresa , Nombre_Base As Name Into #Tmp_Bases From #Tmp_Bases1
Drop table #Tmp_Bases1
Select @Fila = 1
Select @Script_Dinamico = ''
Select @Script_Dinamico1 = ''
Select @Nro_Registros = (Select Count (*) From #Tmp_Bases) + 1--cantidad de filas +1
While @Fila < = @Nro_Registros
Begin
Print(@Script_Dinamico)
Select @Script_Dinamico = 'INSERT INTO buc.dbo.tmp_DAT_CLIENTES '
Select @Script_Dinamico = @Script_Dinamico +'(COD_EMPRESA,'
Select @Script_Dinamico = @Script_Dinamico +'COD_SUCURSALEMPRESA,'
Select @Script_Dinamico = @Script_Dinamico +'RUT_CLIENTE,'
Select @Script_Dinamico = @Script_Dinamico +'RAZON_SOCIAL, '
Select @Script_Dinamico = @Script_Dinamico +'FECHA_CARGA,'
Select @Script_Dinamico = @Script_Dinamico +'FECHA_MODIFICACION'
Select @Script_Dinamico = @Script_Dinamico +')'
Select @Script_Dinamico = @Script_Dinamico +'Select '
Select @Script_Dinamico = @Script_Dinamico +'COD_EMPRESA, '
Select @Script_Dinamico = @Script_Dinamico +'COD_SUCURSALEMPRESA, '
Select @Script_Dinamico = @Script_Dinamico +'RUT_CLIENTE, '
Select @Script_Dinamico = @Script_Dinamico +'RAZON_SOCIAL, '
Select @Script_Dinamico = @Script_Dinamico +'FECHA_CARGA, '
Select @Script_Dinamico = @Script_Dinamico +'FECHA_MODIFICACION'
Select @Script_Dinamico = @Script_Dinamico + ' from ' + (Select Name From #tmp_bases Where Fila = @Fila) +'.Dbo.DAT_CLIENTES '
Select @Script_Dinamico = @Script_Dinamico + 'Where ' + (Select Name From #tmp_bases Where Fila = @Fila) +'.Dbo.DAT_CLIENTES.cod_empresa = '+ convert(varchar,(Select cod_empresa From #tmp_bases Where Fila = @Fila))
Select @Script_Dinamico = @Script_Dinamico +'and '+ (Select Name From #tmp_bases Where Fila = @Fila) +'.Dbo.DAT_CLIENTES.rut_cliente not in (select RUT_CLIENTE from tmp_dat_clientes'
Select @Script_Dinamico = @Script_Dinamico+' where '+ (Select Name From #tmp_bases Where Fila = @Fila) +'.Dbo.DAT_CLIENTES.rut_cliente = buc.dbo.tmp_DAT_CLIENTES.RUT_CLIENTE)'
Print (@Script_Dinamico)
Exec (@Script_Dinamico)
Select @Fila = @Fila +1
Select @Script_Dinamico = ''
Select @Script_Dinamico1 = ''
End
Drop Table #Tmp_Bases
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
您可能想要對所有這些輸出做什麼?來自所有數據庫所有表的所有數據?真? – 2012-08-07 15:44:08
這也假設表都具有完全相同的結構 – swasheck 2012-08-07 15:47:02
是所有表都是相同的結構 – suely 2012-08-07 16:30:06