Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store static data in your SQL Server Database Project in VS 2012

Tags:

I am trying to use the SQL Server Database Project to keep all our table, stored procedure, views etc scripts. I now want a way to be able to keep all our reference (static) data as well. When the tool or project is run it will install all the DB objects and the insert all the reference data.

I found similar articles for vs 2010 but they were using things like Team Edition for Database professionals.

  • Get our DB under source control.
  • Synchronize our local development DB with latest version in source control.
  • Work with Visual Studio 2012 and SQL Server 2012
  • Use .Net tools as far as possible and not something like Redgate (Redgate is great but I don't want to for out for it just yet if I can use tools in VS 2012)
like image 651
uriDium Avatar asked Dec 12 '12 15:12

uriDium


People also ask

How do I add a static row in SQL?

You can add static value when you use INSERT INTO SELECT MySQL query. Write the value directly in the select statement or you can add with the help of variable which initializes the value.

Where we can store the data in SQL Server?

SQL Server databases are stored in the file system in files. Files can be grouped into filegroups.

What is static data in SQL?

Static data, also known as reference or lookup data, is data that is necessary for the first deployment so that the application which is built upon that database can work properly. This data can be any set of predefined values that are rarely changed, e.g. postal codes, lists of states e.g. NY, etc.


1 Answers

You can use this approach:

  • Put your reference data into XML files, one per table
  • Add XML files with reference data to your database project
  • Use a Post-Deployment script to extract the data from XML and merge it into your tables

Here is a more detailed description of each step, illustrated with an example. Let's say that you need to initialize a table of countries that has this structure:

create table Country (     CountryId uniqueidentifier NOT NULL,     CountryCode varchar(2) NOT NULL,     CountryName varchar(254) NOT NULL ) 

Create a new folder called ReferenceData under your database project. It should be a sibling folder of the Schema Objects and Scripts.

Add a new XML file called Country.xml to the ReferenceData folder. Populate the file as follows:

<countries>     <country CountryCode="CA" CountryName="Canada"/>     <country CountryCode="MX" CountryName="Mexico"/>     <country CountryCode="US" CountryName="United States of America"/> </countries> 

Find Script.PostDeployment.sql, and add the following code to it:

DECLARE @h_Country int  DECLARE @xmlCountry xml = N' :r ..\..\ReferenceData\Country.xml '  EXEC sp_xml_preparedocument @h_Country OUTPUT, @xmlCountry  MERGE Country AS target USING (     SELECT c.CountryCode, c.CountryName     FROM OPENXML(@h_Country, '/countries/country', 1)     WITH (CountryCode varchar(2), CountryName varchar(254)) as c) AS source (CountryCode, CountryName) ON (source.CountryCode = target.CountryCode) WHEN MATCHED THEN     UPDATE SET CountryName = source.CountryName WHEN NOT MATCHED BY TARGET THEN     INSERT (CountryId, CountryCode, CountryName) values (newid(), source.CountryCode, source.CountryName) ; 

I tried this solution only in VS 2008, but it should be agnostic to your development environment.

like image 106
Sergey Kalinichenko Avatar answered Oct 09 '22 05:10

Sergey Kalinichenko