Skip to content

Instantly share code, notes, and snippets.

@BrentOzar
Created October 24, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrentOzar/2e3340a9c2ab2a14f8638280efa3ce49 to your computer and use it in GitHub Desktop.
Save BrentOzar/2e3340a9c2ab2a14f8638280efa3ce49 to your computer and use it in GitHub Desktop.
Creating a multi-tenant version of StackOverflow.dbo.Posts
CREATE TABLE [dbo].[Posts_MultiTenant](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CompanyCode] VARCHAR(10) NOT NULL,
[AcceptedAnswerId] [int] NULL,
[AnswerCount] [int] NULL,
[Body] [nvarchar](max) NOT NULL,
[ClosedDate] [datetime] NULL,
[CommentCount] [int] NULL,
[CommunityOwnedDate] [datetime] NULL,
[CreationDate] [datetime] NOT NULL,
[FavoriteCount] [int] NULL,
[LastActivityDate] [datetime] NOT NULL,
[LastEditDate] [datetime] NULL,
[LastEditorDisplayName] [nvarchar](40) NULL,
[LastEditorUserId] [int] NULL,
[OwnerUserId] [int] NULL,
[ParentId] [int] NULL,
[PostTypeId] [int] NOT NULL,
[Score] [int] NOT NULL,
[Tags] [nvarchar](150) NULL,
[Title] [nvarchar](250) NULL,
[ViewCount] [int] NOT NULL,
CONSTRAINT [PK_Posts_MultiTenant_Id] 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
SET IDENTITY_INSERT dbo.Posts_MultiTenant ON;
INSERT INTO dbo.Posts_MultiTenant ([Id]
,CompanyCode
,[AcceptedAnswerId]
,[AnswerCount]
,[Body]
,[ClosedDate]
,[CommentCount]
,[CommunityOwnedDate]
,[CreationDate]
,[FavoriteCount]
,[LastActivityDate]
,[LastEditDate]
,[LastEditorDisplayName]
,[LastEditorUserId]
,[OwnerUserId]
,[ParentId]
,[PostTypeId]
,[Score]
,[Tags]
,[Title]
,[ViewCount])
SELECT [Id]
,'BOU' AS CompanyCode
,[AcceptedAnswerId]
,[AnswerCount]
,[Body]
,[ClosedDate]
,[CommentCount]
,[CommunityOwnedDate]
,[CreationDate]
,[FavoriteCount]
,[LastActivityDate]
,[LastEditDate]
,[LastEditorDisplayName]
,[LastEditorUserId]
,[OwnerUserId]
,[ParentId]
,[PostTypeId]
,[Score]
,[Tags]
,[Title]
,[ViewCount]
FROM [dbo].[Posts]
GO
SET IDENTITY_INSERT dbo.Posts_MultiTenant OFF;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment