Try this - CREATE PROCEDURE SplitTable ( @TableName AS VARCHAR(128) )ASBEGINSET NOCOUNT ON DECLARE @i AS INT, @start AS INT, @cnt AS INT, @Loop AS INT DECLARE @SQL AS VARCHAR(MAX) DECLARE @ColumnNames AS VARCHAR(MAX) SELECT @SQL = '', @i = 5, @Start = 1, @Loop = 1 SET @cnt = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName) WHILE @i <= ( @cnt ) BEGIN SET @ColumnNames = '' SELECT @ColumnNames = @ColumnNames + COLUMN_NAME + ',' FROM INFORMATION_SCHEMA.COLUMNS WHERE ORDINAL_POSITION BETWEEN @start AND @i AND TABLE_NAME = @TableName SET @ColumnNames = LEFT(@ColumnNames, LEN(@ColumnNames)-1) SET @SQL = @SQL + CHAR(13) + 'SELECT ' + @ColumnNames + ' INTO ' + @TableName + CONVERT(VARCHAR(10), @Loop) + ' FROM ' + @TableName SELECT @Loop = @Loop + 1 IF @i = @cnt GOTO Exit_Proc SET @start = @i+1 SET @i = @i + CASE WHEN @cnt - @i >=5 THEN 5 ELSE @cnt - @i END END Exit_Proc: --PRINT ( @SQL ) EXEC ( @SQL )SET NOCOUNT ONENDGOEXEC SplitTable '<Your Table>'
Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER