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
4

1 回答 1

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 子句中的=in 最终会尝试将一个 DB 中的列的值与另一个 DB 中的另一整列进行比较。也许这应该是一个in或其他一些运算符,可以将一个值与一组值进行比较,而不是比较两个单个值。

您是否收到任何错误消息?

于 2012-10-17T18:58:27.013 回答