USE [DatabaseName]
-- Name of Database
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[usp_createclientschema]
@Loginname nvarchar(50), -- Login name of new schema like 'sa' in case of dbo
@LoginPassword nvarchar(50), -- password for new schema
@User nvarchar(50), -- user for schema 'can be same as schema name'
@SchemaName nvarchar(50) -- schema name
AS
BEGIN
declare @queryLoginName nvarchar(max)
declare @queryUser nvarchar(max)
declare @querySchema nvarchar(max)
declare @queryRole1 nvarchar(max)
declare @queryCreateTable nvarchar(max)
declare @authSchema nvarchar(max)
-- This portion create authentication for new schema
select @querySchema = ('CREATE SCHEMA '+ @SchemaName+ ';' ) execute (@querySchema)
select @queryLoginName = ('CREATE LOGIN '+@Loginname+' WITH PASSWORD = '''+@LoginPassword+''';') execute (@queryLoginName)
select @queryUser = ('CREATE USER '+@User+' FOR LOGIN '+@Loginname+' WITH DEFAULT_SCHEMA = ' + @SchemaName + ';') execute (@queryUser)
select @authSchema = ('ALTER AUTHORIZATION ON SCHEMA::' + @SchemaName + ' TO ' + @User + ';')execute (@authSchema)
select @queryRole1 = ('sp_addrolemember ''db_owner'','''+@User +'''') execute (@queryRole1)
--Below portion create table into new schema from dbo, here condition 1=1 will copy complete structure with data and 1=2 with create only structure into schema
select @queryCreateTable =
('
SELECT * INTO '+ @SchemaName+'.Table1 from Table1 where 1 = 1 ;
SELECT * INTO '+ @SchemaName+'.Table2 from Table2 where 1 = 2 ;
')
execute(@queryCreateTable)
-- This portion of code will create Types , for that make stored procedure of Types into dbo database and ----execute them here
execute sp_storedprocedure1_Type @SchemaName
execute sp_storedprocedure2_Type @SchemaName
-- Below portion is to create Clustered index into schema
declare @ClusIndex1 nvarchar(max)
select @ClusIndex1 =
('
ALTER TABLE ['+@SchemaName+'].[Tablename] ADD CONSTRAINT [PK_Columnname] PRIMARY KEY CLUSTERED
(
[AutoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
')
execute (@ClusIndex1)
-- Below portion is to create NonClustered index into schema
declare @NonClusIndex1 nvarchar(max)
select @NonClusIndex1 =
('
CREATE NONCLUSTERED INDEX [Country] ON ['+@SchemaName+'].[tablename]
(
[columnname1] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
')
execute (@NonClusIndex1)
-- Below portion is to create Stored procedure into schema from dbo
DECLARE @SQL1 nvarchar(max) SELECT @SQL1 = REPLACE(OBJECT_DEFINITION (object_id('usp_Storedprocedure1')),'dbo',''+ @SchemaName+'')exec sp_executesql @SQL1
DECLARE @SQL2 nvarchar(max) SELECT @SQL2 = REPLACE(OBJECT_DEFINITION (object_id('usp_Storedprocedure2')),'dbo',''+ @SchemaName+'')exec sp_executesql @SQL2
END