Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Cross Join 'n' volte una tabella

Utilizzando SQL dinamico, SQL Server 2005+ (@table_name e @numCrossJoins sono parametri di stored procedure):

DECLARE @upperLimit INT
    SET @upperLimit = 1

DECLARE @SQL NVARCHAR(MAX)
    SET @SQL = 'SELECT * FROM '+ @table_name +' '

BEGIN 

  WHILE (upperLimit <= @numCrossJoins)
  BEGIN

    SET @SQL = @SQL + 'CROSS JOIN '+ QUOTENAME(@table_name) +' '

    SET @upperLimit = @upperLimit + 1
  END

  EXEC sp_executesql @SQL

END