2012-08-07 60 views
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 
+0

您可能想要對所有這些輸出做什麼?來自所有數據庫所有表的所有數據?真? – 2012-08-07 15:44:08

+0

這也假設表都具有完全相同的結構 – swasheck 2012-08-07 15:47:02

+0

是所有表都是相同的結構 – suely 2012-08-07 16:30:06

回答

0

我假設你在許多數據庫具有相同的表(DAT_CLIENTES),並希望如果數據庫名稱是Estadistica_Cobranza.Dbo.Sis_Conexion_pagos.Nombre_Base上市獲得所有的這些記錄。

雖然確實有很多方法可以做到這一點,但您似乎已經使用while循環,臨時表和動態SQL(所有這些都可能具有性能和維護影響)將它們放在一起。然而,編碼方法和風格之外,我最關心的是這一行:

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)' 

在動態where子句最終會嘗試將列的值在每一個DB與另一個整列比較=在另一個數據庫。也許這應該是in或其他一些可以將值與一組值相比較的運算符,而不是比較兩個單值。

您是否收到任何錯誤消息?

相關問題