Scrip para generar una secuencia de numeros

En ciertas ocasiones necesitamos generar una secuencia de numeros, la cual se puede generar de dos formas, usando cross join o CTE.

Cross Join

;with Base AS (
	SELECT 0 AS val1 
	UNION ALL 
	SELECT 0 
	UNION ALL 
	SELECT 0 
	UNION ALL 
	SELECT 0 
	UNION ALL 
	SELECT 0)
,N2 AS (
SELECT T1.val1 FROM Base AS T1 
CROSS JOIN Base AS T2 
CROSS JOIN Base AS T3)
SELECT TOP 1000 ROW_NUMBER() over (ORDER BY T1.val1) AS rnk_numero 
FROM N2 as T1 
CROSS JOIN Base AS T2 
CROSS JOIN Base AS T3

CTE Common Table Expression

;WITH Sec AS (  
 SELECT 0 AS base
UNION ALL  
 SELECT  base+1 
  FROM  Sec  
  WHERE base < 1000
)  
SELECT base FROM Sec

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *