Skip to main content

Create a new databse in stored procedure (SQL SERVER 2008 R2)

Sometimes one of the business requirements of the system is to create a new databse at the runtime. When You using SQL Server this operation can be done easy and quick. The stored procedure below takes two arguments. First 'DbName ' sets a name for new database and the second one gathers information about success of the operation as true or false. Note that second argument is output type which means that in C# code you can get information about create new database.


Now its time for some magic. After creating a new DB there is no object inside. But You don`t have to move all of structure to new DB each time you create a new one. In SQL Server one of the four database names 'model'. When you move all of Your buissnes logic (tables, views, stored procedures and even data) to it, all the structured will be move to new DB when it`s creating. Looks great, but remeber that if you have some PK on the table with data in model DB during cration of new DB PK values will be reset so any of relation won`t be created and some error may occure. Other problem certainly occur when you using CLR assembly in Your databse and want to copy it to new DB. This because in SQL Server you couldn`t set DB property Trustworthy ON and enable CLR integration.




Hope this help someone...

CREATE PROCEDURE proc_DBCreation
@DbName nvarchar(255),
@Result bit output
AS
BEGIN


DECLARE @LogFile nvarchar(255)
SET @LogFile = @DbName+'_log'


DECLARE @PathMDF nvarchar(255)
SET @PathMDF = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\'+@DbName+'.mdf';


DECLARE @PathLOG nvarchar(255)
SET @PathLOG =  N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\'+@DbName+'_log.ldf';


IF NOT EXISTS(SELECT TOP 1 1 FROM sys.databases WHERE name = @DbName)
BEGIN

Declare @sql nvarchar(max) SET @sql =
'CREATE DATABASE ['+@DbName+'] ON  PRIMARY '+
'( NAME = '''+@DbName+''', FILENAME = N'''+ @PathMDF +''', SIZE = 5120KB , FILEGROWTH = 1024KB ) '+
'LOG ON '+
'( NAME = '''+@LogFile+''', FILENAME = N'''+ @PathLOG+''' ,'+
'SIZE = 1024KB , FILEGROWTH = 10%) '
exec(@sql)

SET @Result = 1;
END
ELSE
SET @Result =0;
END


More info:


Popular posts from this blog

Persisting Enum in database with Entity Framework

Problem statement We all want to write clean code and follow best coding practices. This all engineers 'North Star' goal which in many cases can not be easily achievable because of many potential difficulties with converting our ideas/good practices into working solutions.  One of an example I recently came across was about using ASP.NET Core and Entity Framework 5 to store Enum values in a relational database (like Azure SQL). Why is this a problem you might ask... and my answer here is that you want to work with Enum types in your code but persist an integer in your databases. You can think about in that way. Why we use data types at all when everything could be just a string which is getting converted into a desirable type when needed. This 'all-string' approach is of course a huge anti-pattern and a bad practice for many reasons with few being: degraded performance, increased storage space, increased code duplication.  Pre-requirements 1. Status enum type definition...

Using Newtonsoft serializer in CosmosDB client

Problem In some scenarios engineers might want to use a custom JSON serializer for documents stored in CosmosDB.  Solution In CosmosDBV3 .NET Core API, when creating an instance of  CosmosClient one of optional setting in  CosmosClientOptions is to specify an instance of a Serializer . This serializer must be JSON based and be of  CosmosSerializer type. This means that if a custom serializer is needed this should inherit from CosmosSerializer abstract class and override its two methods for serializing and deserializing of an object. The challenge is that both methods from  CosmosSerializer are stream based and therefore might be not as easy to implement as engineers used to assume - still not super complex.  For demonstration purpose as or my custom serializer I'm going to use Netwonsoft.JSON library. Firstly a new type is needed and this must inherit from  CosmosSerializer.  using  Microsoft.Azure.Cosmos; using  Newtonsoft.Json; usin...

Multithread processing of the SqlDataReader - Producer/Consumer design pattern

In today post I want to describe how to optimize usage of a ADO.NET SqlDataReader class by using multi-threading. To present that lets me introduce a problem that I will try to solve.  Scenario : In a project we decided to move all data from a multiple databases to one data warehouse. It will be a good few terabytes of data or even more. Data transfer will be done by using a custom importer program. Problem : After implementing a database agnostic logic of generating and executing a query I realized that I can retrieve data from source databases faster that I can upload them to big data store through HTTP client -importer program. In other words, data reader is capable of reading data faster then I can process it an upload to my big data lake. Solution : As a solution for solving this problem I would like to propose one of a multi-thread design pattern called Producer/Consumer . In general this pattern consists of a two main classes where: Producer class is res...