1 DECLARE @i INT = 0 2 DECLARE @Count INT = 6 3 DECLARE @sql NVARCHAR(MAX) 4 WHILE (@i < @Count) 5 BEGIN 6 DECLARE @iStr NVARCHAR(10) = CAST(@i AS NVARCHAR(10)) 7 SET @sql = 'IF OBJECT_ID(N''PushQueue_' + @iStr + ''', N''U'') IS NULL 8 begin 9 SELECT * INTO [PushQueue_' + @iStr + '] FROM dbo.PushQueue WHERE 1=0 10 alter table [PushQueue_' + @iStr + '] add CONSTRAINT PK_PushQueue_' + @iStr + ' primary key(ID) 11 end 12 ' 13 PRINT @sql 14 SET @i = @i + 1 15 END