Sunday, September 20, 2009

Using Computed Columns in SQL Server

When it comes to database design in SQL Server, it is always recommended to create the database as flexible as possible. SQL Server provides several features for developers in order to be able to do so.

An example of such a feature are the "computed" and "persisted" columns.
As explained on MSDN BOL, computed columns' values are computed by expressions that can use other columns in the same table.

Also, when using the PERSISTED keyword with a COMPUTED column, the data of the computed column is physically stored in the table.

Let's see some code and try out this great feature!

First let's create two tables representing product information:

--Note the column with name "totalPrice": It is a computed column which computes
--the total price (price + VAT)

CREATE TABLE Product1
(

id int,
code varchar(50),
price float,

vatPerc float,
totalPrice as (price+(price*(vatPerc/100)))

)

GO

--Note again the column with name "totalPrice": It is the same computed column as in table
--Product 1 with the difference that it uses the PERSISTED keyword.

CREATE TABLE Product2
(

id int,
code varchar(50),

price float,

vatPerc float,
totalPrice as (price+(price*(vatPerc/100))) PERSISTED

)

GO


------------ Testing Table Product1 --------------
INSERT INTO Product1 (id,code,price,vatPerc)
SELECT 1, 'PRODUCT01',100,15
UNION ALL

SELECT 2, 'PRODUCT02',200,15

GO


* I know, I could use a Table Value Constructor (Row Constructors) but I would prefer that my examples to be compatible with all versions of SQL Server as Table Value Constructors is one of the new programmability features in SQL Server 2008 :)

Note that in the above INSERT statement I am not inserting any values for the "totalPrice" column. Though, when taking a look at the generated results, the totalPrice values are calculated on the fly!

SELECT *
FROM Product1

GO


Results
-------

Similarly, testing table Product2 will work the same way with the only difference that this time,
the calculated values for the "totalPrice" column will be physically stored in the table:

------------ Testing Table Product2 --------------
INSERT INTO Product2 (id,code,price,vatPerc)
SELECT 1, 'PRODUCT03',300,15
UNION ALL

SELECT 2, 'PRODUCT04',400,15

GO


SELECT *
FROM Product2

GO


Results
-------

* Note: By the time you are using a Computed column you cannot explicitly insert data into it. The data is computed based on the computed column's definition (expression).
Read more on this article...

Saturday, September 19, 2009

Friends of Red Gate

Red Gate is a software company based in the university town of Cambridge, UK, specializing in MS SQL Server, .NET and email archiving tools.

Red Gate is one of the strong supporters of the SQL Server, .NET and Exchange communities. A great example of this, are Red Gate's well-known community web sites www.simple-talk.com and www.sqlservercentral.com.

Red Gate, takes their relationship with the SQL Server, .NET and Exchange communities very seriously. A solid example of this, is the "Friends of Red Gate" program. The program is aimed at active and influential community members who are experts and gurus in their relevant fields.

Through the program, the members have the chance to use and review Red Gate's tools thus being able helping others who are having problems which could be solved using these tools.

As a new member of the program, I am very excited to participate as I am given the chance to use these great tools, review them and provide my feedback! I am already using SQL Prompt 4 and SQLRefactor and loving it!

Subsequent posts will follow, reviewing and discussing Red Gate's tools mainly having to do with SQL Server and .NET Development.

A big thanks to Red Gate for being a true supporter of the technical community!
Read more on this article...

Sunday, September 13, 2009

Mysore PASS Chapter SQL Server Webcasts Series

Newsflash: More free SQL Server training is on the way!

The Mysore/Mandya Technology Group (MY-TG) organizes a series of SQL Server Webcasts which started on Saturday, September 12th, 2009 and will be taking place every Saturday for the next few weeks. To this end, every Saturday, two sessions are presented.

Topics include SQL Server 2008 Administration, Development and Data Access. This is another great opportunity to learn more about trending SQL Server topics.

I have the pleasure to participate as a speaker to this event on Saturday, September 19th, 2009, 12:30 - 13:30 (UTC) where I will be presenting the session with topic: "Flexible data synchronization with Microsoft Sync Framework".

In this session, among other, we will talk about the fundamentals of Data Synchronization and how we can easily build from scratch a flexible Sync Ecosystem using Microsoft Sync Framework 2.0 CTP2, thus allowing the smooth operation of an Occasionally Connected System.

Click here to register for this event and for more details please visit http://positivekingdom.info.

See you online!
Read more on this article...

Tuesday, September 8, 2009

Accessing SQL Azure Using ADO.NET

In my first post on SQL Azure (SQL Azure: A First Contact) I talked about how you can access SQL Azure using SQL Server 2008 Management Studio (SSMS).

In this post, the journey continues with “walking” on the cloud using ADO .NET :)

Here’s the recipe!

Ingredients
  • A valid SQL Azure CTP account
  • Visual Studio 2008 SP1
  • .NET Framework 3.5 SP1
  • C# (or respective VB.NET) namespace: System.Data.SqlClient
Objects used from System.Data.SqlClient namespace:
  • SqlConnection
  • SqlCommand
  • SqlDataReader
  • SqlDataAdapter
Process
  • Step 1: Add namespace for using SqlClient
  • Step 2: Set up connection to database
  • Step 3: Open database connection
  • Step 4: Set the T-SQL command(s) - Use the "SqlCommand" object
  • Step 5: Open the SqlDataReader - Execute the T-SQL command(s)
  • Step 6: Display the results (if any)
  • Step 7: Close the SqlDataReader
  • Step 8: Close the database connection
Source Code (for obvious reasons I removed the connection string from the source code file)
  • You can find the sample source code for this post here.
  • *Note: This sample code is for demo purposes and does not represent production code.
Considerations
  • My example is based on the example in my previous post on SQL Azure (Database Name: sqlazure, table name: CLOUD_MSGS)
  • For this post I used a new Visual C# “Console Application” project named ADO .NET – SQL Azure like in the following screenshot:


Screenshot of the output


Have fun!

Drop me a comment if you need any help implementing the ADO.NET connection to SQL Azure!
Read more on this article...

Friday, September 4, 2009

SQL Azure: A First Contact

SQL Azure… Yep, that is SQL Server on the Cloud!

A few days ago I received my SQL Azure (CTP) invitation code. The first thing I naturally did right after this was to create and access a database on the Cloud! I was very curious to see how I could interact with a database hosted on the cloud.

After getting access to the SQL Azure Service (you can register for the CTP here), you can easily create a database by clicking on the “Create Database” button.

The default database is the well-known “master” database.
I called the database I created “sqlazure”.

So, an empty database is created, what next? One of the options is to connect to the database using SQL Server 2008 Management Studio (SSMS)!

I will provide the procedure I followed (with screenshots) in steps:


Step 1
Select the database hosted on the cloud in SQL Azure and click on the “Connection Strings” button:


When clicking on that button you will get three connection strings for the following:
  • ADO .NET
  • ODBC
  • OLE DB
In all the connection strings, you can find the same server value in the form of Server=tcp:[full_server_name]. Note down the full server name or just copy it to your Windows clipboard.

Step 2
Start SQL Server 2008 Management Studio (SSMS).
On the connection dialog that appears when starting SSMS, click on the “Cancel” button.



Step 3
In SSMS click on “New Query”. The connection dialog appears again.

On the connection dialog enter the following information:
  • Server Name: The full server name as provided by the SQL Azure connection string.
  • Authentication: SQL Server Authentication.
  • Login: The SQL Azure server admin username.
  • Password: The SQL Azure server admin password.

Also, before clicking the “Connect” button, click on “Options” and under the “Connection Properties” tab, in the “Connect to database” field, enter the database name which is hosted on the cloud and you want to access*.

* In this case you have to note that if you do not specify the database in the connection properties, you will be automatically connected to the “master” database. Though, once connected to a database on SQL Azure, it is not yet possible to use the “USE” command for switching to another database. The only way for switching to another database, is to establish a new connection on that database.

OK, after selecting the database on which you want to connect (in this example sqlazure) click on the “Connect” button.


By the way, you will get an error message about ANSI_NULLS but you can ignore this and continue.


That’s it! You are now connected to your database hosted on the cloud and you can issue queries using SSMS query window!

Step 4
Finally, let’s run some queries against our database:

-- Create a table
CREATE TABLE CLOUD_MSGS(
msgID int,
msgDescr varchar(100)
)
GO

-- Create unique clustered index
CREATE UNIQUE CLUSTERED INDEX msgID_ind
ON CLOUD_MSGS (msgID)
GO

-- Populate table with data ... using Row Constructors! :)
INSERT INTO CLOUD_MSGS(msgID,msgDescr)
VALUES (1,'Welcome to SQL Azure'), (2,'Hello World!'), (3,'SQL is on the Cloud!')
GO

-- Display the contents of the "CLOUD_MSGS" table
SELECT *
FROM CLOUD_MSGS


Resources

Below you can find some useful resources regarding SQL Azure. Note that this is pre-release documentation and is subject to change in future releases:

Read more on this article...