I am having problem with my script. at first time, it runs fine, but when second time, when their is no data in schema ABC tables, it still make change to original schema tables. here is code. I think, it is bypassing step2 or 3.DECLARE TCursor CURSOR FOR --- (1) get table names from [dbo].[Tablelist]SELECT [TableName] from [dbo].[Tablelist] where SchemaName = 'abc' --have 5 schemas, but only need 3DECLARE @TableName varchar(255)OPEN TCursorFETCH NEXT FROM TCursor INTO @TableNameWHILE @@FETCH_STATUS = 0BEGIN DECLARE @Stmt3 nvarchar(max) --(2) check if jobhistory table have new date and batchjob status is success.which is on server1SET @Stmt3 = ('select tablename FROM .[dbo].[JobHistory] where tableName = '''+ (@TableName) + '''and FLSLoadedDate > (getdate() -1) and JobBatchStatus <>''fail') ---(2)BEGIN---(3) also need to check if table has records inserted. which is on server2SET @Stmt3 = ('If (Exists (Select * From abc.' +@tablename + '))' )BEGIN--(4) if records are in schema abc, flip original schema. otherwise leave original schema as it is.SET @Stmt3 = 'ALTER SCHEMA abc1 TRANSFER Original.' +@tablename EXEC (@Stmt3) SET @Stmt3 = 'ALTER SCHEMA Original TRANSFER abc.'+@tablename EXEC (@Stmt3) SET @Stmt3 = 'ALTER SCHEMA abc TRANSFER abc1.' +@tablename EXEC (@Stmt3) SET @Stmt3 = 'TRUNCATE TABLE abc.' +@tablename EXEC (@Stmt3)END FETCH NEXT FROM TCursor INTO @TableName END ENDCLOSE TCursorDEALLOCATE TCursorthanks,Navie
↧