I'm duplicating a series of tables using a loop in SQL. I've inferred from SQL Server Maximum rows that can be inserted in a single insert statment that the maximum rows which can be inserted using a select *
is 10,000 for my SQL SERVER. However, the source tables are bigger than 10,000 rows. Is there a way to bypass this limitation without explicitly stating the columns in the select statement, since these will be different for each table?
DECLARE @iTable TABLE (TableName VARCHAR(50),Id int identity(1,1))
INSERT INTO @iTable
Select distinct table_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'D0%' OR table_name like 'D1%'
DECLARE @imax int
DECLARE @iSQL VARCHAR(MAX)
DECLARE @iTableName VARCHAR(max)
DECLARE @iid int = 1
select @imax = MAX(Id) from @iTable
WHILE (@iid <= @imax)
BEGIN
SELECT @iTableName = TableName FROM @iTable WHERE Id = @iid
SET @iSQL = 'select * into st_'+ @iTableName +' from '+ @iTableName +';'
EXEC(@iSQL)
SET @iid = @iid +1
END