text: ﻿/*
  Deployment script for DatawarehouseSucos
  
  This code was generated by a tool.
  Changes to this file may cause incorrect behavior and will be lost if
  the code is regenerated.
  */
  
  GO
  SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
  
  SET NUMERIC_ROUNDABORT OFF;
  
  
  GO
  :setvar DatabaseName "DatawarehouseSucos"
  :setvar DefaultFilePrefix "DatawarehouseSucos"
  :setvar DefaultDataPath ""
  :setvar DefaultLogPath ""
  
  GO
  :on error exit
  GO
  /*
  Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
  To re-enable the script after enabling SQLCMD mode, execute the following:
  SET NOEXEC OFF; 
  */
  :setvar __IsSqlCmdEnabled "True"
  GO
  IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
      BEGIN
          PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
          SET NOEXEC ON;
      END
  
  
  GO
  USE [master];
  
  
  GO
  
  IF (DB_ID(N'$(DatabaseName)') IS NOT NULL) 
  BEGIN
      ALTER DATABASE [$(DatabaseName)]
      SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
      DROP DATABASE [$(DatabaseName)];
  END
  
  GO
  PRINT N'Creating database $(DatabaseName)...'
  GO
  CREATE DATABASE [$(DatabaseName)] COLLATE SQL_Latin1_General_CP1_CI_AS
  GO
  USE [$(DatabaseName)];
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET ANSI_NULLS ON,
                  ANSI_PADDING ON,
                  ANSI_WARNINGS ON,
                  ARITHABORT ON,
                  CONCAT_NULL_YIELDS_NULL ON,
                  NUMERIC_ROUNDABORT OFF,
                  QUOTED_IDENTIFIER ON,
                  ANSI_NULL_DEFAULT ON,
                  CURSOR_DEFAULT LOCAL,
                  RECOVERY FULL,
                  CURSOR_CLOSE_ON_COMMIT OFF,
                  AUTO_CREATE_STATISTICS ON,
                  AUTO_SHRINK OFF,
                  AUTO_UPDATE_STATISTICS ON,
                  RECURSIVE_TRIGGERS OFF 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET ALLOW_SNAPSHOT_ISOLATION OFF;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET READ_COMMITTED_SNAPSHOT OFF 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET AUTO_UPDATE_STATISTICS_ASYNC OFF,
                  PAGE_VERIFY NONE,
                  DATE_CORRELATION_OPTIMIZATION OFF,
                  DISABLE_BROKER,
                  PARAMETERIZATION SIMPLE,
                  SUPPLEMENTAL_LOGGING OFF 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF IS_SRVROLEMEMBER(N'sysadmin') = 1
      BEGIN
          IF EXISTS (SELECT 1
                     FROM   [master].[dbo].[sysdatabases]
                     WHERE  [name] = N'$(DatabaseName)')
              BEGIN
                  EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
      SET TRUSTWORTHY OFF,
          DB_CHAINING OFF 
      WITH ROLLBACK IMMEDIATE';
              END
      END
  ELSE
      BEGIN
          PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
      END
  
  
  GO
  IF IS_SRVROLEMEMBER(N'sysadmin') = 1
      BEGIN
          IF EXISTS (SELECT 1
                     FROM   [master].[dbo].[sysdatabases]
                     WHERE  [name] = N'$(DatabaseName)')
              BEGIN
                  EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
      SET HONOR_BROKER_PRIORITY OFF 
      WITH ROLLBACK IMMEDIATE';
              END
      END
  ELSE
      BEGIN
          PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
      END
  
  
  GO
  ALTER DATABASE [$(DatabaseName)]
      SET TARGET_RECOVERY_TIME = 0 SECONDS 
      WITH ROLLBACK IMMEDIATE;
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF),
                  CONTAINMENT = NONE 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF),
                  MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = OFF,
                  DELAYED_DURABILITY = DISABLED 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET QUERY_STORE (QUERY_CAPTURE_MODE = ALL, DATA_FLUSH_INTERVAL_SECONDS = 900, INTERVAL_LENGTH_MINUTES = 60, MAX_PLANS_PER_QUERY = 200, CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 367), MAX_STORAGE_SIZE_MB = 100) 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET QUERY_STORE = OFF 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
          ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
          ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
          ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
          ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
          ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
          ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
          ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
      END
  
  
  GO
  IF EXISTS (SELECT 1
             FROM   [master].[dbo].[sysdatabases]
             WHERE  [name] = N'$(DatabaseName)')
      BEGIN
          ALTER DATABASE [$(DatabaseName)]
              SET TEMPORAL_HISTORY_RETENTION ON 
              WITH ROLLBACK IMMEDIATE;
      END
  
  
  GO
  IF fulltextserviceproperty(N'IsFulltextInstalled') = 1
      EXECUTE sp_fulltext_database 'enable';
  
  
  GO
  PRINT N'Creating Table [dbo].[DIM_Fabrica]...';
  
  
  GO
  CREATE TABLE [dbo].[DIM_Fabrica] (
      [Cod_Fabrica]  NVARCHAR (50)  NOT NULL,
      [Desc_Fabrica] NVARCHAR (200) NULL,
      PRIMARY KEY CLUSTERED ([Cod_Fabrica] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[DIM_Categoria]...';
  
  
  GO
  CREATE TABLE [dbo].[DIM_Categoria] (
      [Cod_Categoria]  NVARCHAR (50)  NOT NULL,
      [Desc_Categoria] NVARCHAR (200) NULL,
      PRIMARY KEY CLUSTERED ([Cod_Categoria] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Dim_Organizacional]...';
  
  
  GO
  CREATE TABLE [dbo].[Dim_Organizacional] (
      [Cod_Filho]  NVARCHAR (50)  NOT NULL,
      [Desc_Filho] NVARCHAR (200) NULL,
      [Cod_Pai]    NVARCHAR (50)  NULL,
      [Esquerda]   INT            NULL,
      [Direita]    INT            NULL,
      [Nivel]      INT            NULL,
      PRIMARY KEY CLUSTERED ([Cod_Filho] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Dim_Tempo]...';
  
  
  GO
  CREATE TABLE [dbo].[Dim_Tempo] (
      [Cod_Dia]            NVARCHAR (50) NOT NULL,
      [Data]               DATE          NULL,
      [Cod_Semana]         INT           NULL,
      [Nome_Dia_Semana]    NVARCHAR (50) NULL,
      [Cod_Mes]            INT           NULL,
      [Nome_Mes]           NVARCHAR (50) NULL,
      [Cod_Mes_Ano]        NVARCHAR (50) NULL,
      [Nome_Mes_Ano]       NVARCHAR (50) NULL,
      [Cod_Trimestre]      INT           NULL,
      [Nome_Trimestre]     NVARCHAR (50) NULL,
      [Cod_Trimestre_Ano]  NVARCHAR (50) NULL,
      [Nome_Trimestre_Ano] NVARCHAR (50) NULL,
      [Cod_Semestre]       INT           NULL,
      [Nome_Semestre]      NVARCHAR (50) NULL,
      [Cod_Semestre_Ano]   NVARCHAR (50) NULL,
      [Nome_Semestre_Ano]  NVARCHAR (50) NULL,
      [Ano]                NVARCHAR (50) NULL,
      [Tipo_Dia]           NVARCHAR (50) NULL,
      PRIMARY KEY CLUSTERED ([Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Dim_Produto]...';
  
  
  GO
  CREATE TABLE [dbo].[Dim_Produto] (
      [Cod_Produto]  NVARCHAR (50)  NOT NULL,
      [Desc_Produto] NVARCHAR (200) NULL,
      [Atr_Tamanho]  NVARCHAR (200) NULL,
      [Atr_Sabor]    NVARCHAR (200) NULL,
      [Cod_Marca]    NVARCHAR (50)  NULL,
      PRIMARY KEY CLUSTERED ([Cod_Produto] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[DIM_Marca]...';
  
  
  GO
  CREATE TABLE [dbo].[DIM_Marca] (
      [Cod_Marca]     NVARCHAR (50)  NOT NULL,
      [Desc_Marca]    NVARCHAR (200) NULL,
      [Cod_Categoria] NVARCHAR (50)  NULL,
      PRIMARY KEY CLUSTERED ([Cod_Marca] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Fato_001]...';
  
  
  GO
  CREATE TABLE [dbo].[Fato_001] (
      [Cod_Cliente]        NVARCHAR (50) NOT NULL,
      [Cod_Produto]        NVARCHAR (50) NOT NULL,
      [Cod_Organizacional] NVARCHAR (50) NOT NULL,
      [Cod_Fabrica]        NVARCHAR (50) NOT NULL,
      [Cod_Dia]            NVARCHAR (50) NOT NULL,
      [Faturamento]        FLOAT (53)    NULL,
      [Imposto]            FLOAT (53)    NULL,
      [Custo_Variavel]     FLOAT (53)    NULL,
      [Quantidade_Vendida] FLOAT (53)    NULL,
      [Unidade_Vendida]    FLOAT (53)    NULL,
      PRIMARY KEY CLUSTERED ([Cod_Produto] ASC, [Cod_Cliente] ASC, [Cod_Organizacional] ASC, [Cod_Fabrica] ASC, [Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Fato_005]...';
  
  
  GO
  CREATE TABLE [dbo].[Fato_005] (
      [Cod_Produto] NVARCHAR (50) NOT NULL,
      [Cod_Fabrica] NVARCHAR (50) NOT NULL,
      [Cod_Dia]     NVARCHAR (50) NOT NULL,
      [Meta_Custo]  FLOAT (53)    NULL,
      PRIMARY KEY CLUSTERED ([Cod_Produto] ASC, [Cod_Fabrica] ASC, [Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Fato_003]...';
  
  
  GO
  CREATE TABLE [dbo].[Fato_003] (
      [Cod_Fabrica] NVARCHAR (50) NOT NULL,
      [Cod_Dia]     NVARCHAR (50) NOT NULL,
      [Frete]       FLOAT (53)    NULL,
      PRIMARY KEY CLUSTERED ([Cod_Fabrica] ASC, [Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Dim_Cliente]...';
  
  
  GO
  CREATE TABLE [dbo].[Dim_Cliente] (
      [Cod_Cliente]   NVARCHAR (50)  NOT NULL,
      [Desc_Cliente]  NVARCHAR (200) NULL,
      [Cod_Cidade]    NVARCHAR (50)  NULL,
      [Desc_Cidade]   NVARCHAR (200) NULL,
      [Cod_Estado]    NVARCHAR (50)  NULL,
      [Desc_Estado]   NVARCHAR (200) NULL,
      [Cod_Regiao]    NVARCHAR (50)  NULL,
      [Desc_Regiao]   NVARCHAR (200) NULL,
      [Cod_Segmento]  NVARCHAR (50)  NULL,
      [Desc_Segmento] NVARCHAR (200) NULL,
      PRIMARY KEY CLUSTERED ([Cod_Cliente] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Fato_002]...';
  
  
  GO
  CREATE TABLE [dbo].[Fato_002] (
      [Cod_Cliente] NVARCHAR (50) NOT NULL,
      [Cod_Produto] NVARCHAR (50) NOT NULL,
      [Cod_Fabrica] NVARCHAR (50) NOT NULL,
      [Cod_Dia]     NVARCHAR (50) NOT NULL,
      [Frete]       FLOAT (53)    NULL,
      PRIMARY KEY CLUSTERED ([Cod_Produto] ASC, [Cod_Cliente] ASC, [Cod_Fabrica] ASC, [Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Table [dbo].[Fato_004]...';
  
  
  GO
  CREATE TABLE [dbo].[Fato_004] (
      [Cod_Cliente]        NVARCHAR (50) NOT NULL,
      [Cod_Produto]        NVARCHAR (50) NOT NULL,
      [Cod_Organizacional] NVARCHAR (50) NOT NULL,
      [Cod_Dia]            NVARCHAR (50) NOT NULL,
      [Meta_Faturamento]   FLOAT (53)    NULL,
      PRIMARY KEY CLUSTERED ([Cod_Produto] ASC, [Cod_Cliente] ASC, [Cod_Organizacional] ASC, [Cod_Dia] ASC)
  );
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Dim_Organizacional_Dim_Organizacional]...';
  
  
  GO
  ALTER TABLE [dbo].[Dim_Organizacional]
      ADD CONSTRAINT [FK_Dim_Organizacional_Dim_Organizacional] FOREIGN KEY ([Cod_Pai]) REFERENCES [dbo].[Dim_Organizacional] ([Cod_Filho]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Dim_Produto_DIM_Marca]...';
  
  
  GO
  ALTER TABLE [dbo].[Dim_Produto]
      ADD CONSTRAINT [FK_Dim_Produto_DIM_Marca] FOREIGN KEY ([Cod_Marca]) REFERENCES [dbo].[DIM_Marca] ([Cod_Marca]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Dim_Marca_DIM_Categoria]...';
  
  
  GO
  ALTER TABLE [dbo].[DIM_Marca]
      ADD CONSTRAINT [FK_Dim_Marca_DIM_Categoria] FOREIGN KEY ([Cod_Categoria]) REFERENCES [dbo].[DIM_Categoria] ([Cod_Categoria]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_001_DIM_Cliente]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_001]
      ADD CONSTRAINT [FK_Fato_001_DIM_Cliente] FOREIGN KEY ([Cod_Cliente]) REFERENCES [dbo].[Dim_Cliente] ([Cod_Cliente]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_001_Dim_Produto]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_001]
      ADD CONSTRAINT [FK_Fato_001_Dim_Produto] FOREIGN KEY ([Cod_Produto]) REFERENCES [dbo].[Dim_Produto] ([Cod_Produto]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_001_Dim_Organizacional]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_001]
      ADD CONSTRAINT [FK_Fato_001_Dim_Organizacional] FOREIGN KEY ([Cod_Organizacional]) REFERENCES [dbo].[Dim_Organizacional] ([Cod_Filho]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_001_DIM_Fabrica]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_001]
      ADD CONSTRAINT [FK_Fato_001_DIM_Fabrica] FOREIGN KEY ([Cod_Fabrica]) REFERENCES [dbo].[DIM_Fabrica] ([Cod_Fabrica]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_001_Dim_Tempo]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_001]
      ADD CONSTRAINT [FK_Fato_001_Dim_Tempo] FOREIGN KEY ([Cod_Dia]) REFERENCES [dbo].[Dim_Tempo] ([Cod_Dia]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_005_Dim_Produto]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_005]
      ADD CONSTRAINT [FK_Fato_005_Dim_Produto] FOREIGN KEY ([Cod_Produto]) REFERENCES [dbo].[Dim_Produto] ([Cod_Produto]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_005_DIM_Fabrica]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_005]
      ADD CONSTRAINT [FK_Fato_005_DIM_Fabrica] FOREIGN KEY ([Cod_Fabrica]) REFERENCES [dbo].[DIM_Fabrica] ([Cod_Fabrica]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_005_Dim_Tempo]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_005]
      ADD CONSTRAINT [FK_Fato_005_Dim_Tempo] FOREIGN KEY ([Cod_Dia]) REFERENCES [dbo].[Dim_Tempo] ([Cod_Dia]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_003_Dim_Fabrica]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_003]
      ADD CONSTRAINT [FK_Fato_003_Dim_Fabrica] FOREIGN KEY ([Cod_Fabrica]) REFERENCES [dbo].[DIM_Fabrica] ([Cod_Fabrica]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_003_Dim_Tempo]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_003]
      ADD CONSTRAINT [FK_Fato_003_Dim_Tempo] FOREIGN KEY ([Cod_Dia]) REFERENCES [dbo].[Dim_Tempo] ([Cod_Dia]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_002_DIM_Cliente]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_002]
      ADD CONSTRAINT [FK_Fato_002_DIM_Cliente] FOREIGN KEY ([Cod_Cliente]) REFERENCES [dbo].[Dim_Cliente] ([Cod_Cliente]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_002_Dim_Produto]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_002]
      ADD CONSTRAINT [FK_Fato_002_Dim_Produto] FOREIGN KEY ([Cod_Produto]) REFERENCES [dbo].[Dim_Produto] ([Cod_Produto]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_002_DIM_Fabrica]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_002]
      ADD CONSTRAINT [FK_Fato_002_DIM_Fabrica] FOREIGN KEY ([Cod_Fabrica]) REFERENCES [dbo].[DIM_Fabrica] ([Cod_Fabrica]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_002_Dim_Tempo]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_002]
      ADD CONSTRAINT [FK_Fato_002_Dim_Tempo] FOREIGN KEY ([Cod_Dia]) REFERENCES [dbo].[Dim_Tempo] ([Cod_Dia]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_004_DIM_Cliente]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_004]
      ADD CONSTRAINT [FK_Fato_004_DIM_Cliente] FOREIGN KEY ([Cod_Cliente]) REFERENCES [dbo].[Dim_Cliente] ([Cod_Cliente]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_004_Dim_Produto]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_004]
      ADD CONSTRAINT [FK_Fato_004_Dim_Produto] FOREIGN KEY ([Cod_Produto]) REFERENCES [dbo].[Dim_Produto] ([Cod_Produto]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_004_Dim_Organizacional]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_004]
      ADD CONSTRAINT [FK_Fato_004_Dim_Organizacional] FOREIGN KEY ([Cod_Organizacional]) REFERENCES [dbo].[Dim_Organizacional] ([Cod_Filho]);
  
  
  GO
  PRINT N'Creating Foreign Key [dbo].[FK_Fato_004_Dim_Tempo]...';
  
  
  GO
  ALTER TABLE [dbo].[Fato_004]
      ADD CONSTRAINT [FK_Fato_004_Dim_Tempo] FOREIGN KEY ([Cod_Dia]) REFERENCES [dbo].[Dim_Tempo] ([Cod_Dia]);
  
  
  GO
  -- Refactoring step to update target server with deployed transaction logs
  
  IF OBJECT_ID(N'dbo.__RefactorLog') IS NULL
  BEGIN
      CREATE TABLE [dbo].[__RefactorLog] (OperationKey UNIQUEIDENTIFIER NOT NULL PRIMARY KEY)
      EXEC sp_addextendedproperty N'microsoft_database_tools_support', N'refactoring log', N'schema', N'dbo', N'table', N'__RefactorLog'
  END
  GO
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '12d70c61-d2f5-4801-9d3f-823d393d9e1d')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('12d70c61-d2f5-4801-9d3f-823d393d9e1d')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = 'f3889e47-4ce0-497c-825c-9686ec4e2395')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('f3889e47-4ce0-497c-825c-9686ec4e2395')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '9d31198b-7561-4620-89c4-3f053e602d4d')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('9d31198b-7561-4620-89c4-3f053e602d4d')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '588585f2-3d04-4b74-abc4-8fb695144e03')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('588585f2-3d04-4b74-abc4-8fb695144e03')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '2c219341-a7db-41ff-9fc5-b3b31e59c20d')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('2c219341-a7db-41ff-9fc5-b3b31e59c20d')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '9175cee3-c1e6-4f83-803c-3d09cf6df262')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('9175cee3-c1e6-4f83-803c-3d09cf6df262')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = 'e2aa485b-f97f-4ddd-8a96-d1b17ff32b31')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('e2aa485b-f97f-4ddd-8a96-d1b17ff32b31')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '41eeed6c-dc37-4b29-a38f-07a981d59878')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('41eeed6c-dc37-4b29-a38f-07a981d59878')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '8f0625c3-cd8b-47fc-8234-aeae2d29559d')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('8f0625c3-cd8b-47fc-8234-aeae2d29559d')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = 'fae3856e-9c65-4879-a756-16c42aa988a5')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('fae3856e-9c65-4879-a756-16c42aa988a5')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = 'b66e7316-c103-4fe4-afd0-8ec1372274fe')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('b66e7316-c103-4fe4-afd0-8ec1372274fe')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '8e473cc1-63e9-4269-a1cb-78339839a927')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('8e473cc1-63e9-4269-a1cb-78339839a927')
  IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '4a242a63-356a-4b60-a57d-135b543ef566')
  INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('4a242a63-356a-4b60-a57d-135b543ef566')
  
  GO
  
  GO
  DECLARE @VarDecimalSupported AS BIT;
  
  SELECT @VarDecimalSupported = 0;
  
  IF ((ServerProperty(N'EngineEdition') = 3)
      AND (((@@microsoftversion / power(2, 24) = 9)
            AND (@@microsoftversion & 0xffff >= 3024))
           OR ((@@microsoftversion / power(2, 24) = 10)
               AND (@@microsoftversion & 0xffff >= 1600))))
      SELECT @VarDecimalSupported = 1;
  
  IF (@VarDecimalSupported > 0)
      BEGIN
          EXECUTE sp_db_vardecimal_storage_format N'$(DatabaseName)', 'ON';
      END
  
  
  GO
  ALTER DATABASE [$(DatabaseName)]
      SET MULTI_USER 
      WITH ROLLBACK IMMEDIATE;
  
  
  GO
  PRINT N'Update complete.';
  
  
  GO
  