﻿GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;

declare @sql nvarchar(max) 
select @sql = 'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + ' DROP  CONSTRAINT ' + d.name from sys.default_constraints d
inner join sys.columns c ON c.column_id = d.parent_column_id and c.object_id = d.parent_object_id
WHERE parent_object_id = OBJECT_ID('dbo.T_MUNKAUGYIADATOK') AND c.name = 'C_ISORAADO'

exec sp_executesql @sql

GO
PRINT N'Altering [dbo].[T_MUNKAUGYIADATOK]...';


GO
IF(col_length('[dbo].[T_MUNKAUGYIADATOK]', '[C_ISORAADO]')) IS NOT NULL BEGIN
	ALTER TABLE [dbo].[T_MUNKAUGYIADATOK] DROP COLUMN [C_ISORAADO];
END

GO
PRINT N'Refreshing [dbo].[T_MUNKAUGYIADATOK_OSSZES]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[T_MUNKAUGYIADATOK_OSSZES]';


GO
PRINT N'Update complete.';


GO


PRINT '--- intézményi view-k (dbo táblákkal megegyező nevű és a tábla_OSSZES nevűek) frissítése ---'

DECLARE  @viewname nvarchar(100), @pureview nvarchar(100)

DECLARE semaviewk CURSOR LOCAL FOR 
  SELECT '['+TABLE_SCHEMA + '].[' + TABLE_NAME + ']', TABLE_NAME
  FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'VIEW' and TABLE_SCHEMA LIKE 'KR[_]%[_]Schema'
  AND TABLE_NAME in (
  'T_MUNKAUGYIADATOK', 'T_MUNKAUGYIADATOK_OSSZES'
  ) -- ha csak néhány VIEW frissült, akkor a gyorsabb futásért ide fel lehet sorolni
  ORDER BY TABLE_SCHEMA, TABLE_NAME

OPEN semaviewk
FETCH NEXT FROM semaviewk INTO @viewname, @pureview

WHILE @@FETCH_STATUS = 0 BEGIN
	PRINT @viewname
	EXEC sp_refreshview @viewname

    FETCH NEXT FROM semaviewk INTO @viewname, @pureview
END             

CLOSE semaviewk
DEALLOCATE semaviewk
GO
