我正在使用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 :(得分: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中的列的值与另一个DB中的另一个整列进行比较。也许这应该是in
或其他一些可以将值与一组值进行比较的运算符,而不是比较两个单值。
您收到任何错误消息吗?