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 If there are no errors or if there is any other error other than 208 iI need to delete that from the table .But 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 y iwhy I created this SP. Any other ideas are also appreciated. I am new to coding so please forgive any mistakes iI'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
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
END ELSE print 'no objects' END