SCHEMA PREVIEW

SERVICE_RES_USERS

Xem trước DDL trước khi tạo hoặc bổ sung cột an toàn trên SQL Server.

Quay lại

SQL Server DDL

Chỉ tự động tạo schema/table/index và thêm cột nullable còn thiếu.

IF SCHEMA_ID(N'stg') IS NULL EXEC(N'CREATE SCHEMA [stg]');
IF SCHEMA_ID(N'ods') IS NULL EXEC(N'CREATE SCHEMA [ods]');
IF SCHEMA_ID(N'hist') IS NULL EXEC(N'CREATE SCHEMA [hist]');
IF OBJECT_ID(N'stg.J4_res_users', N'U') IS NULL
BEGIN
    CREATE TABLE [stg].[J4_res_users]
    (
        [id] int NULL,
        [company_id] int NULL,
        [partner_id] int NULL,
        [active] bit NULL,
        [create_date] datetime2(6) NULL,
        [login] nvarchar(max) NULL,
        [password] nvarchar(max) NULL,
        [action_id] int NULL,
        [create_uid] int NULL,
        [write_uid] int NULL,
        [signature] nvarchar(max) NULL,
        [share] bit NULL,
        [write_date] datetime2(6) NULL,
        [totp_secret] nvarchar(max) NULL,
        [notification_type] nvarchar(max) NULL,
        [odoobot_state] nvarchar(max) NULL,
        [odoobot_failed] bit NULL,
        [sale_team_id] int NULL,
        [oauth_provider_id] int NULL,
        [oauth_uid] nvarchar(max) NULL,
        [oauth_access_token] nvarchar(max) NULL,
        [uuid] nvarchar(max) NULL,
        [website_id] int NULL,
        [target_sales_won] int NULL,
        [target_sales_done] int NULL,
        [target_sales_invoiced] int NULL,
        [sync_source_id] int NOT NULL,
        [sync_job_id] int NOT NULL,
        [sync_run_id] bigint NOT NULL,
        [sync_row_hash] varbinary(32) NOT NULL,
        [sync_is_deleted] bit NOT NULL,
        [sync_created_at] datetime2(7) NOT NULL,
        [sync_updated_at] datetime2(7) NOT NULL
    );
END;
IF OBJECT_ID(N'ods.res_users', N'U') IS NULL
BEGIN
    CREATE TABLE [ods].[res_users]
    (
        [id] int NOT NULL,
        [company_id] int NOT NULL,
        [partner_id] int NOT NULL,
        [active] bit NULL,
        [create_date] datetime2(6) NULL,
        [login] nvarchar(max) NOT NULL,
        [password] nvarchar(max) NULL,
        [action_id] int NULL,
        [create_uid] int NULL,
        [write_uid] int NULL,
        [signature] nvarchar(max) NULL,
        [share] bit NULL,
        [write_date] datetime2(6) NULL,
        [totp_secret] nvarchar(max) NULL,
        [notification_type] nvarchar(max) NOT NULL,
        [odoobot_state] nvarchar(max) NULL,
        [odoobot_failed] bit NULL,
        [sale_team_id] int NULL,
        [oauth_provider_id] int NULL,
        [oauth_uid] nvarchar(max) NULL,
        [oauth_access_token] nvarchar(max) NULL,
        [uuid] nvarchar(max) NULL,
        [website_id] int NULL,
        [target_sales_won] int NULL,
        [target_sales_done] int NULL,
        [target_sales_invoiced] int NULL,
        [sync_source_id] int NOT NULL,
        [sync_job_id] int NOT NULL,
        [sync_run_id] bigint NOT NULL,
        [sync_row_hash] varbinary(32) NOT NULL,
        [sync_is_deleted] bit NOT NULL CONSTRAINT [DF_res_users_IsDeleted] DEFAULT (0),
        [sync_created_at] datetime2(7) NOT NULL,
        [sync_updated_at] datetime2(7) NOT NULL,
        CONSTRAINT [PK_res_users] PRIMARY KEY ([sync_source_id], [id])
    );
END;
IF OBJECT_ID(N'hist.J4_res_users_History', N'U') IS NULL
BEGIN
    CREATE TABLE [hist].[J4_res_users_History]
    (
        [history_id] bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
        [source_record_id] nvarchar(450) NOT NULL,
        [id] int NULL,
        [company_id] int NULL,
        [partner_id] int NULL,
        [active] bit NULL,
        [create_date] datetime2(6) NULL,
        [login] nvarchar(max) NULL,
        [password] nvarchar(max) NULL,
        [action_id] int NULL,
        [create_uid] int NULL,
        [write_uid] int NULL,
        [signature] nvarchar(max) NULL,
        [share] bit NULL,
        [write_date] datetime2(6) NULL,
        [totp_secret] nvarchar(max) NULL,
        [notification_type] nvarchar(max) NULL,
        [odoobot_state] nvarchar(max) NULL,
        [odoobot_failed] bit NULL,
        [sale_team_id] int NULL,
        [oauth_provider_id] int NULL,
        [oauth_uid] nvarchar(max) NULL,
        [oauth_access_token] nvarchar(max) NULL,
        [uuid] nvarchar(max) NULL,
        [website_id] int NULL,
        [target_sales_won] int NULL,
        [target_sales_done] int NULL,
        [target_sales_invoiced] int NULL,
        [sync_source_id] int NOT NULL,
        [sync_job_id] int NOT NULL,
        [sync_run_id] bigint NOT NULL,
        [valid_from] datetime2(7) NOT NULL,
        [valid_to] datetime2(7) NULL,
        [is_current] bit NOT NULL,
        [row_hash] varbinary(32) NOT NULL,
        [source_updated_at] datetimeoffset(7) NOT NULL,
        [sync_is_deleted] bit NOT NULL
    );
END;
IF COL_LENGTH(N'stg.J4_res_users', N'id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'company_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [company_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'company_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [company_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'company_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [company_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'partner_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [partner_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'partner_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [partner_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'partner_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [partner_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'active') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [active] bit NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'active') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [active] bit NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'active') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [active] bit NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'create_date') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [create_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'create_date') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [create_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'create_date') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [create_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'login') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [login] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'login') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [login] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'login') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [login] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'password') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [password] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'password') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [password] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'password') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [password] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'action_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [action_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'action_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [action_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'action_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [action_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'create_uid') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [create_uid] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'create_uid') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [create_uid] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'create_uid') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [create_uid] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'write_uid') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [write_uid] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'write_uid') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [write_uid] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'write_uid') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [write_uid] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'signature') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [signature] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'signature') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [signature] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'signature') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [signature] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'share') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [share] bit NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'share') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [share] bit NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'share') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [share] bit NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'write_date') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [write_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'write_date') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [write_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'write_date') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [write_date] datetime2(6) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'totp_secret') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [totp_secret] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'totp_secret') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [totp_secret] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'totp_secret') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [totp_secret] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'notification_type') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [notification_type] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'notification_type') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [notification_type] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'notification_type') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [notification_type] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'odoobot_state') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [odoobot_state] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'odoobot_state') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [odoobot_state] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'odoobot_state') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [odoobot_state] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'odoobot_failed') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [odoobot_failed] bit NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'odoobot_failed') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [odoobot_failed] bit NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'odoobot_failed') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [odoobot_failed] bit NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'sale_team_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [sale_team_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'sale_team_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [sale_team_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'sale_team_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [sale_team_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'oauth_provider_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [oauth_provider_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'oauth_provider_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [oauth_provider_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'oauth_provider_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [oauth_provider_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'oauth_uid') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [oauth_uid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'oauth_uid') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [oauth_uid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'oauth_uid') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [oauth_uid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'oauth_access_token') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [oauth_access_token] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'oauth_access_token') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [oauth_access_token] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'oauth_access_token') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [oauth_access_token] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'uuid') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [uuid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'uuid') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [uuid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'uuid') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [uuid] nvarchar(max) NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'website_id') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [website_id] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'website_id') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [website_id] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'website_id') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [website_id] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'target_sales_won') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [target_sales_won] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'target_sales_won') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [target_sales_won] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'target_sales_won') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [target_sales_won] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'target_sales_done') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [target_sales_done] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'target_sales_done') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [target_sales_done] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'target_sales_done') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [target_sales_done] int NULL;
END;
IF COL_LENGTH(N'stg.J4_res_users', N'target_sales_invoiced') IS NULL
BEGIN
    ALTER TABLE [stg].[J4_res_users] ADD [target_sales_invoiced] int NULL;
END;
IF COL_LENGTH(N'ods.res_users', N'target_sales_invoiced') IS NULL
BEGIN
    ALTER TABLE [ods].[res_users] ADD [target_sales_invoiced] int NULL;
END;
IF COL_LENGTH(N'hist.J4_res_users_History', N'target_sales_invoiced') IS NULL
BEGIN
    ALTER TABLE [hist].[J4_res_users_History] ADD [target_sales_invoiced] int NULL;
END;
IF NOT EXISTS
(
    SELECT 1
    FROM sys.indexes AS indexes
    INNER JOIN sys.index_columns AS first_column
        ON first_column.[object_id] = indexes.[object_id]
       AND first_column.[index_id] = indexes.[index_id]
       AND first_column.[key_ordinal] = 1
    INNER JOIN sys.columns AS first_column_name
        ON first_column_name.[object_id] = first_column.[object_id]
       AND first_column_name.[column_id] = first_column.[column_id]
    INNER JOIN sys.index_columns AS second_column
        ON second_column.[object_id] = indexes.[object_id]
       AND second_column.[index_id] = indexes.[index_id]
       AND second_column.[key_ordinal] = 2
    INNER JOIN sys.columns AS second_column_name
        ON second_column_name.[object_id] = second_column.[object_id]
       AND second_column_name.[column_id] = second_column.[column_id]
    WHERE indexes.[object_id] = OBJECT_ID(N'ods.res_users')
      AND first_column_name.[name] = N'sync_source_id'
      AND second_column_name.[name] = N'id'
)
    CREATE INDEX [IX_res_users_SyncSource_Key] ON [ods].[res_users] ([sync_source_id], [id]);
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'ods.res_users') AND name = N'IX_res_users_UpdatedAt_Id')
    CREATE INDEX [IX_res_users_UpdatedAt_Id] ON [ods].[res_users] ([write_date], [id]);
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'ods.res_users') AND name = N'IX_res_users_IsDeleted')
    CREATE INDEX [IX_res_users_IsDeleted] ON [ods].[res_users] ([sync_is_deleted]);
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'hist.J4_res_users_History') AND name = N'IX_J4_res_users_History_Record')
    CREATE INDEX [IX_J4_res_users_History_Record] ON [hist].[J4_res_users_History] ([sync_source_id], [source_record_id], [is_current]);