Trang tài liệu Trí Nam

Debug là việc khó gấp đôi code. Nếu viết code thông minh quá mức, bạn sẽ không đủ thông minh để debug nó

Database Conventions

Estimated reading: 19 minutes 725 views

Nguyên tắc chung

  1. Mỗi một dịch vụ một database riêng
  2. Nguyên tắc đặt tên database <Scope của dự án>_<Môi trường>_<Mã dịch vụ>_<Phiên bản core>
    Ví dụ: với dự án thuộc scope U202001, môi trường developement, mã dịch vụ là CanBo, phiên bản core 5.0 thì tên database sẽ là U202001_Dev_CanBo_V5
  3. Mỗi một dự án sẽ có 1 tài khoản truy cập riêng, tên tài khoản chính là tên scope của dự án (ví dụ U202001)
  4. Tên các bảng được đặt bằng tiếng Việt không dấu, có thể đặt tên theo nhóm chức năng
    Ví dụ: CanBo_HoSo_ChucDanh_BoNhiem_Dot – Bảng lưu trữ danh sách các đợt bổ nhiệm, thuộc nhóm chức năng cha là quản lý chức danh cán bộ, và nhóm chức năng cao hơn nữa là hồ sơ cán bộ
  5. Các cột dạng số hoặc bit khuyến cáo để not null và set giá trị mặc định (trừ khi nghiệp vụ yêu cầu phải có null)
    Ví dụ cột ThuTuHienThi, kiểu int thì đặt giá trị mặc định = 0
  6. Nguyên tắc đặt tên database, tên bảng và tên field: Viết hoa chữ cái đầu của các từ
    Ví dụ: CanBo_HoSo_ChucDanh_BoNhiem_Dot
  7. Khi tạo CSDL trừ các field base ra thì cần điền description vào tất cả các field. Việc điền description này sẽ là đầu vào cho code gen CRUD. Xem thêm thông tin về code gen tại đây
    Xem hướng dẫn hiển thị cột Description khi design cột trong bảng tại đây
  8. Khoá ngoại đặt tên theo nguyên tắc Id<Tên bản liên kết khoá ngoại> và bỏ hết các dấu _ (nếu có)
    Ví dụ: Trong bảng CanBo_HoiDong_ThanhVien tạo 1 field tên là IdCanBoHoiDong để liên kết với bảng CanBo_HoiDong

Nguyên tắc đặt tên

  1. Tên bảng 
    – Đặt theo kiểu pascal case
       VD: NhapHocDot
    – Chỉ sử dụng ký tự _ cho những bảng dạng mapping
       VD: NhapHocDot_ThanhPhanHoSo
    – Những bảng danh mục thêm tiền tố DM
       VD: DMDoiTuongDaoTao
  2. Tên cột
    – Đặt theo kiểu pascal case
       VD: HoVaTen
    – Cột liên kết cùng database Id<Tên bảng>
       VD: IdHoSo
    – Cột liên kết khác database Id<Tên database><Tên bảng>
       VD: IdNguoiHocHoSo

Các cột thuộc base (bảng nào cũng cần có các cột này)

Danh sách môi trường

Những scripts thường dùng

1. Tạo các bảng base

				
					-- Tạo các bảng base
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EntityMetadatas](
[Id] [uniqueidentifier] NOT NULL,
[Code] [varchar](100) NOT NULL,
[Created] [datetime2](7) NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[CreatedBy] [varchar](36) NULL,
[ModifiedBy] [varchar](36) NULL,
[IsDeleted] [bit] NOT NULL,
[Entity] [varchar](100) NOT NULL,
[Value] [nvarchar](max) NOT NULL,
[EntityInstanceId] [uniqueidentifier] NULL,
[IsBuildIn] [bit] NOT NULL,
[IsBuildInAll] [bit] NOT NULL,
[ServiceCode] [nvarchar](100) NULL,
[EntityKey] [nvarchar](100) NULL,
[Version] [int] NOT NULL,
[CurrentStateCode] [varchar](255) NULL,
[UserIdLastInWorkflow] [varchar](100) NULL,
CONSTRAINT [PK_EntityMetadatas] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[EntityPermissions] Script Date: 4/8/2021 4:15:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EntityPermissions](
[Id] [uniqueidentifier] NOT NULL,
[Table] [varchar](50) NULL,
[UserIds] [varchar](2048) NULL,
[Filters] [nvarchar](2048) NULL,
[Clause] [nvarchar](2048) NULL,
[Created] [datetime2](7) NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[IsDeleted] [bit] NOT NULL,
[CreatedBy] [varchar](36) NULL,
[ModifiedBy] [varchar](36) NULL,
[Code] [varchar](100) NULL,
[IsBuildIn] [bit] NOT NULL,
[IsBuildInAll] [bit] NOT NULL,
[ServiceCode] [nvarchar](100) NULL,
[Entity] [nvarchar](100) NULL,
[EntityKey] [nvarchar](100) NULL,
[Version] [int] NOT NULL,
[CurrentStateCode] [varchar](255) NULL,
[UserIdLastInWorkflow] [varchar](100) NULL,
CONSTRAINT [PK_EntityPermissions] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[EntityWorkflowHistories] Script Date: 4/8/2021 4:15:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EntityWorkflowHistories](
[Id] [uniqueidentifier] NOT NULL,
[CreatedBy] [varchar](36) NOT NULL,
[Created] [datetime2](7) NOT NULL,
[ActionType] [int] NOT NULL,
[Note] [nvarchar](200) NULL,
[Entity] [varchar](100) NOT NULL,
[EntityId] [int] NOT NULL,
[IsBuildIn] [bit] NOT NULL,
[IsBuildInAll] [bit] NOT NULL,
[ServiceCode] [nvarchar](100) NULL,
[EntityKey] [nvarchar](100) NULL,
[Version] [int] NOT NULL,
[CurrentStateCode] [varchar](255) NULL,
[UserIdLastInWorkflow] [varchar](100) NULL,
[Code] [varchar](100) NULL,
[Modified] [datetime2](7) NOT NULL,
[ModifiedBy] [varchar](36) NULL,
[IsDeleted] [bit] NOT NULL,
CONSTRAINT [PK_EntityWorkflowHistories] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[EntityWorkflowInstances] Script Date: 4/8/2021 4:15:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EntityWorkflowInstances](
[Id] [uniqueidentifier] NOT NULL,
[Created] [datetime2](7) NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[IsDeleted] [bit] NOT NULL,
[CreatedBy] [varchar](36) NULL,
[ModifiedBy] [varchar](36) NULL,
[IsBuildIn] [bit] NOT NULL,
[IsBuildInAll] [bit] NOT NULL,
[ServiceCode] [nvarchar](100) NULL,
[Entity] [nvarchar](100) NULL,
[EntityKey] [nvarchar](100) NULL,
[Version] [int] NOT NULL,
[CurrentStateCode] [varchar](255) NOT NULL,
[HanXuLy] [date] NULL,
[CountOfApproved] [int] NOT NULL,
[TotalCountOfApprove] [int] NOT NULL,
[WfType] [int] NOT NULL,
[Partners] [varchar](max) NOT NULL,
[Code] [varchar](100) NULL,
[UserIdLastInWorkflow] [varchar](100) NULL,
CONSTRAINT [PK_EntityWorkflowInstances] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[EntityWorkflowSettings] Script Date: 4/8/2021 4:15:29 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EntityWorkflowSettings](
[Id] [uniqueidentifier] NOT NULL,
[Created] [datetime2](7) NOT NULL,
[Modified] [datetime2](7) NOT NULL,
[IsDeleted] [bit] NOT NULL,
[CreatedBy] [varchar](36) NULL,
[ModifiedBy] [varchar](36) NULL,
[IsBuildIn] [bit] NOT NULL,
[IsBuildInAll] [bit] NOT NULL,
[ServiceCode] [nvarchar](100) NULL,
[Entity] [nvarchar](100) NULL,
[EntityKey] [nvarchar](100) NULL,
[Version] [int] NOT NULL,
[WfType] [int] NOT NULL,
[WfSettings] [nvarchar](max) NOT NULL,
[CurrentStateCode] [varchar](255) NULL,
[Code] [varchar](100) NULL,
[UserIdLastInWorkflow] [varchar](100) NULL,
CONSTRAINT [PK_EntityWorkflowSettings] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF_EntityMetadatas_InstanceId] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF_EntityMetadatas_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF_EntityMetadatas_Modified] DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF_EntityMetadatas_IsDeleted] DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF__EntityMet__IsBui__5AE46118] DEFAULT ((0)) FOR [IsBuildIn]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF__EntityMet__IsBui__30B91D22] DEFAULT ((0)) FOR [IsBuildInAll]
GO
ALTER TABLE [dbo].[EntityMetadatas] ADD CONSTRAINT [DF__EntityMet__Versi__493AB229] DEFAULT ((-1)) FOR [Version]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF_EntityPermission_InstanceId] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF_EntityPermission_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF_EntityPermission_Modified] DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF_EntityPermission_IsDeleted] DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF__EntityPer__IsBui__2A81EC36] DEFAULT ((0)) FOR [IsBuildIn]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF__EntityPer__IsBui__2B76106F] DEFAULT ((0)) FOR [IsBuildInAll]
GO
ALTER TABLE [dbo].[EntityPermissions] ADD CONSTRAINT [DF__EntityPer__Versi__2C6A34A8] DEFAULT ((-1)) FOR [Version]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD CONSTRAINT [DF_EntityWorkflowHistories_Id] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD CONSTRAINT [DF__EntityWor__IsBui__2121D3D7] DEFAULT ((0)) FOR [IsBuildIn]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD CONSTRAINT [DF__EntityWor__IsBui__67B44C51] DEFAULT ((0)) FOR [IsBuildInAll]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD CONSTRAINT [DF__EntityWor__Versi__26268016] DEFAULT ((-1)) FOR [Version]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[EntityWorkflowHistories] ADD DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_InstanceId] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_Modified] DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_IsDeleted] DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_IsBuildIn] DEFAULT ((0)) FOR [IsBuildIn]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_IsBuildInAll] DEFAULT ((0)) FOR [IsBuildInAll]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_Version] DEFAULT ((-1)) FOR [Version]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_CountOfApprove] DEFAULT ((0)) FOR [CountOfApproved]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_TotalCountOfApprove] DEFAULT ((0)) FOR [TotalCountOfApprove]
GO
ALTER TABLE [dbo].[EntityWorkflowInstances] ADD CONSTRAINT [DF_EntityWorkflowInstances_WfType] DEFAULT ((1)) FOR [WfType]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_InstanceId] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_Created] DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_Modified] DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_IsDeleted] DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_IsBuildIn] DEFAULT ((0)) FOR [IsBuildIn]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_IsBuildInAll] DEFAULT ((0)) FOR [IsBuildInAll]
GO
ALTER TABLE [dbo].[EntityWorkflowSettings] ADD CONSTRAINT [DF_EntityWorkflowSettings_Version] DEFAULT ((-1)) FOR [Version]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Mã Entity' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityMetadatas', @level2type=N'COLUMN',@level2name=N'Entity'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Mã trạng thái hiện tại' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'CurrentStateCode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Hạn xử lý' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'HanXuLy'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Số lần đã phê duyệt__Trạng thái của quy trình (ở bước số bao nhiêu)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'CountOfApproved'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Tổng số lần cần phê duyệt' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'TotalCountOfApprove'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Mã loại quy trình' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'WfType'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Danh sách các instance id user có thực hiện thao tác trên bản ghi' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowInstances', @level2type=N'COLUMN',@level2name=N'Partners'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Mã loại quy trình' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowSettings', @level2type=N'COLUMN',@level2name=N'WfType'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Cấu hình quy trình' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'EntityWorkflowSettings', @level2type=N'COLUMN',@level2name=N'WfSettings'
GO
				
			



2. Tạo các cột base

				
					EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''IsBuildIn'')
begin
ALTER TABLE ? ADD IsBuildIn BIT NOT NULL DEFAULT (0);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''IsBuildInAll'')
begin
ALTER TABLE ? ADD IsBuildInAll BIT NOT NULL DEFAULT (0);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''Code'')
begin
ALTER TABLE ? ADD Code VARCHAR(100);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''CurrentStateCode'')
begin
ALTER TABLE ? ADD CurrentStateCode VARCHAR(255);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''Created'')
begin
ALTER TABLE ? ADD Created DATETIME2(7) NOT NULL DEFAULT (getdate());
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''Modified'')
begin
ALTER TABLE ? ADD Modified DATETIME2(7) NOT NULL DEFAULT (getdate());
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''CreatedBy'')
begin
ALTER TABLE ? ADD CreatedBy VARCHAR(36);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''ModifiedBy'')
begin
ALTER TABLE ? ADD ModifiedBy VARCHAR(36);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''IsDeleted'')
begin
ALTER TABLE ? ADD IsDeleted Bit NOT NULL DEFAULT (0);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''ServiceCode'')
begin
ALTER TABLE ? ADD ServiceCode NVARCHAR(100);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''Entity'')
begin
ALTER TABLE ? ADD Entity NVARCHAR(100);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''EntityKey'')
begin
ALTER TABLE ? ADD EntityKey NVARCHAR(100);
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''Version'')
begin
ALTER TABLE ? ADD Version INT NOT NULL DEFAULT ((-1));
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''UserIdLastInWorkflow'')
begin
ALTER TABLE ? ADD UserIdLastInWorkflow VARCHAR(36);
end';

EXEC sp_MSforeachtable '
if exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''PersistedId'')
begin
ALTER TABLE ? DROP COLUMN PersistedId;
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''PersistedId'')
begin
ALTER TABLE ? ADD PersistedId INT NULL;
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''PrintFileId'')
begin
ALTER TABLE ? ADD PrintFileId VARCHAR(100) NULL;
end';

EXEC sp_MSforeachtable '
if not exists (select * from sys.columns
where object_id = object_id(''?'')
and name = ''WorkflowCoreStatus'')
begin
ALTER TABLE ? ADD WorkflowCoreStatus int NOT NULL DEFAULT (0);
end';
				
			


3. Tạo các field base cho một tập các bảng (tạo type, tạo stored và gọi stored)

				
					-- Tạo các field base cho một tập các bảng (tạo type, tạo stored và gọi stored)
CREATE TYPE dbo.TableAndSchemaList
AS TABLE
(
TABLE_NAME VARCHAR(255),
TABLE_SCHEMA VARCHAR(255)
);
GO

ALTER PROC CREATE_BASE_COLUMN
@Tables AS dbo.TableAndSchemaList READONLY
AS
DECLARE @TABLE_NAME varchar(255)
DECLARE @TABLE_SCHEMA varchar(255)
DECLARE cur CURSOR FOR SELECT TABLE_NAME, TABLE_SCHEMA FROM @Tables
OPEN cur
FETCH NEXT FROM cur INTO @TABLE_NAME, @TABLE_SCHEMA

DECLARE @sql NVARCHAR(MAX);
DECLARE @tableFullName NVARCHAR(255);
WHILE @@FETCH_STATUS = 0 BEGIN
SET @tableFullName = @TABLE_SCHEMA + '.' + @TABLE_NAME;
IF COL_LENGTH (@tableFullName, 'InstanceId') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD InstanceId uniqueidentifier NOT NULL DEFAULT newid() ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'Created') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD Created DATETIME2(7) NOT NULL DEFAULT getdate() ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'Modified') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD Modified DATETIME2(7) NOT NULL DEFAULT getdate() ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'CreatedBy') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD CreatedBy VARCHAR(36) NULL ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'ModifiedBy') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD ModifiedBy VARCHAR(36) NULL ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'IsDeleted') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD IsDeleted bit NOT NULL DEFAULT 0';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'Code') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD Code VARCHAR(255) NULL ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'IsBuildIn') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD IsBuildIn BIT NOT NULL DEFAULT 0 ';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'IsBuildInAll') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD IsBuildInAll BIT NOT NULL DEFAULT 0';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'ServiceCode') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD ServiceCode nvarchar(100) NULL';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'Entity') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD Entity nvarchar(100) NULL';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'EntityKey') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD EntityKey nvarchar(100) NULL';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'Version') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD Version INT DEFAULT -1';
EXEC sp_executesql @sql;
END;

IF COL_LENGTH (@tableFullName, 'CurrentStateCode') IS NULL
BEGIN
SET @sql = 'ALTER TABLE ' + @tableFullName + ' ADD CurrentStateCode varchar(255) NULL';
EXEC sp_executesql @sql;
END;
FETCH NEXT FROM cur INTO @TABLE_NAME, @TABLE_SCHEMA
END

CLOSE cur
DEALLOCATE cur

GO

DECLARE @data TableAndSchemaList
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('Event','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('ExecutionError','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('ExecutionPointer','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('ExtensionAttribute','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('Subscription','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('Workflow','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('WorkflowDefine','wfc')
INSERT @data (TABLE_NAME, TABLE_SCHEMA) VALUES ('WorkflowSteps','wfc')

exec CREATE_BASE_COLUMN @data;
				
			
CONTENTS