0

enter image description here

I have this table with 3 records. 3 records are create scripts. I need to execute the create scripts and if there is a 208 error occurred then I need to keep the record and set status as 1. Then execute it again after other records are finished. If there are no errors or if there is any other error other than 208 I need to delete that from the table. But when I execute this it goes on an infinite loop. How do I fix this?

The thing is 2nd view has a reference on 1st one. So I need to create second one 1st and then the 1st one. That's why I created this SP. Any other ideas are also appreciated. I am new to coding so please forgive any mistakes I've made.

Create PROCEDURE [dbo].[UP_CREATE_SCRIPT]

AS 

BEGIN 

declare @qry1 nvarchar(max)
DECLARE @i INT =1
DECLARE @qry2 NVARCHAR(100) 
IF EXISTS (select 1 from [SmartMigrateDB_New].dbo.temp3 where id>0 and type<>'sq') 

BEGIN 
truncate  table error_log

    while (select count(1) from [SmartMigrateDB_New].dbo.temp3 where status=1)>=0

    BEGIN 
        WHILE @i <=  (SELECT MAX(ID) FROM [SmartMigrateDB_New].dbo.temp3)
           BEGIN
            BEGIN TRY
             set @qry1 = ( select code from [SmartMigrateDB_New].dbo.temp3  where id = @i  )
             print @qry1                        
             EXEC sp_ExecuteSQL @qry1
             update [SmartMigrateDB_New].dbo.temp3 set status=0  where id = @i
            END TRY


            BEGIN CATCH
             if ERROR_NUMBER()=208
             update [SmartMigrateDB_New].dbo.temp3 set status=1  where id = @i
             if ERROR_NUMBER()<>208
             update [SmartMigrateDB_New].dbo.temp3 set status=0   where id = @i

             insert into error_log(name,error_no,msg)
             SELECT 
             ERROR_PROCEDURE() as name,
             ERROR_NUMBER() AS error_no,
             ERROR_MESSAGE() AS msg
   
            END CATCH
            
            SET @qry2=(SELECT NAME FROM [SmartMigrateDB_New].dbo.temp3 where id = @i    )
            delete from [SmartMigrateDB_New].dbo.temp3 where id=@i and  status=0  
            print @qry2 +' Created'
            set @i = @i + 1 
           END
    END

END
ELSE print 'no objects'
END
0

1 Answer 1

5

When you're looping over identifiers with non-sequential values, you need to treat the incrementing differently. Here's a quick example:

DECLARE
    @t table (id int PRIMARY KEY);

INSERT 
    @t
(
    id
)
SELECT 
    ABS(ao.object_id)
FROM sys.all_objects AS ao;

DECLARE
    @min bigint,
    @max bigint;

SELECT
    @min = MIN(t.id),
    @max = MAX(t.id)
FROM @t AS t;

WHILE @min < @max
BEGIN
    RAISERROR('Current value: %I64d', 0, 1, @min) WITH NOWAIT;
    
    SELECT TOP (1)
        @min = t.id
    FROM @t AS t
    WHERE t.id > @min
    ORDER BY t.id;
END;

This will find the next identifier that is greater than the current identifier without incrementing to values that don't exist between them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.