Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/02/2013, 09:07
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Consulta Recursiva

aqui creo que necesitas mas bien ejecutar un query dinamico...algo por este estilo


Código SQL:
Ver original
  1. DECLARE @YEAR INT
  2. DECLARE @inicio INT
  3. DECLARE @query VARCHAR(MAX)
  4.  
  5. SET @inicio=2012
  6. SET @YEAR=datepart(yyyy,getdate())
  7. SET @query='select * from apun ' + CONVERT(VARCHAR(20),@inicio) UNION '
  8. while @inicio<=@year
  9. begin
  10. if @inicio=@year
  11. set @query=@query + 'SELECT * FROM apun ' + convert(varchar(20),@inicio)'
  12. ELSE
  13. SET @query='select * from apun ' + CONVERT(VARCHAR(20),@inicio) UNION '
  14.  
  15. set @inicion=@inicio+1
  16. end
  17.  
  18. exec sp_sqlexec @query

saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me