﻿-- View-k törlése, ahol trölve lett már a tábla
DECLARE @sql nvarchar(max) = ''
SELECT @sql += N'DROP VIEW ' + 	QUOTENAME(TABLE_SCHEMA) + N'.' + QUOTENAME(TABLE_NAME) + N';' + char(13) + char(10) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME IN (
     'T_DBVERSION', 'T_FOGLALKOZASOK_TANAROK', 'T_SZOVEGESERTEKELESEK', 'T_SZOVEGESERTEKELESEK_OSSZES', 'T_OSZTALYZATERTEKELES', 'T_OSZTALYZATERTEKELES_OSSZES'
    ,'T_TANARTARGYATLAG','T_TARGYOSZTALYATLAG', 'T_TARGYTANULOATLAG', 'T_DBVERSION_OSSZES'
  ) 
  AND TABLE_TYPE = 'VIEW'


EXEC sp_executesql @sql
print @sql
GO


-- View-k frissítése
DECLARE ViewCursor CURSOR FOR 
SELECT N'EXEC sp_refreshview ''' + 	QUOTENAME(TABLE_SCHEMA) + N'.' + QUOTENAME(TABLE_NAME) + N''';' + char(13) + char(10) 
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'

DECLARE @sql nvarchar(max)

OPEN ViewCursor
FETCH NEXT FROM ViewCursor into @sql
WHILE @@FETCH_STATUS = 0 BEGIN
	PRINT @sql
  EXEC sys.sp_executesql @sql
	FETCH NEXT FROM ViewCursor into @sql 
END

CLOSE ViewCursor
DEALLOCATE ViewCursor
GO
