IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'mnrBillIntegration' AND COLUMN_NAME = 'bCoreFlag')
BEGIN
    EXEC('ALTER TABLE [mnrBillIntegration] ADD [bCoreFlag] bit');
    
    EXEC('
    ALTER VIEW [dbo].[mnrVWBillIntegration]
    AS
    SELECT 
        CASE SystemFlag
            WHEN 1 THEN ''Vertex''
            WHEN 2 THEN ''DTTS'' 
            WHEN 3 THEN ''E-Invoice-EGP'' 
            WHEN 4 THEN ''E-Invoice-SY''
            WHEN 5 THEN ''E-Invoice-SAR'' 
            WHEN 6 THEN ''E-Fatura-TRY'' 
        END AS SystemName,
        bint.*  
    FROM [dbo].[mnrBillIntegration] bint
    ');
END
ELSE
BEGIN
    EXEC('
    ALTER VIEW [dbo].[mnrVWBillIntegration]
    AS
    SELECT 
        CASE SystemFlag
            WHEN 1 THEN ''Vertex''
            WHEN 2 THEN ''DTTS'' 
            WHEN 3 THEN ''E-Invoice-EGP'' 
            WHEN 4 THEN ''E-Invoice-SY''
            WHEN 5 THEN ''E-Invoice-SAR'' 
            WHEN 6 THEN ''E-Fatura-TRY'' 
        END AS SystemName,
        bint.*  
    FROM [dbo].[mnrBillIntegration] bint
    ');
END
GO


UPDATE MNRBILLINTEGRATION SET bCoreFlag=1