You are on page 1of 15

Which are new data types introduced in SQL SERVER 2008? 1. 2.

The GEOMETRY Type: The GEOMETRY data type is a system .NET common language runtime (CLR) data type in SQL Server. This type represents data in a two-dimensional Euclidean coordinate system. The GEOGRAPHY Type: The GEOGRAPHY datatypes functions are the same as with GEOMETRY. The difference between the two is that when you specify GEOGRAPHY, you are usually specifying points in terms of latitude and longitude. New Date and Time Datatypes: SQL Server 2008 introduces four new datatypes related to date and time: DATE, TIME, DATETIMEOFFSET, and DATETIME2. 1. DATE: The new DATE type just stores the date itself. It is based on the Gregorian calendar and handles years from 1 to 9999. 2. TIME: The new TIME (n) type stores time with a range of 00:00:00.0000000 through 23:59:59.9999999. The precision is allowed with this type. TIME supports seconds down to 100 nanoseconds. The n in TIME (n) defines this level of fractional second precision, from 0 to 7 digits of precision. 3. The DATETIMEOFFSET Type: DATETIMEOFFSET (n) is the time-zone-aware version of a datetimedatatype. The name will appear less odd when you consider what it really is: a date + a time + a time-zone offset. The offset is based on how far behind or ahead you are from Coordinated Universal Time (UTC) time. 4. The DATETIME2 Type: It is an extension of the datetime type in earlier versions of SQL Server. This new datatype has a date range covering dates from January 1 of year 1 through December 31 of year 9999. This is a definite improvement over the 1753 lower boundary of the datetimedatatype. DATETIME2 not only includes the larger date range, but also has a timestamp and the same fractional precision that TIME type provides

3.

What are the Advantages of using CTE? 1. 2. 3. 4. Using CTE improves the readability and makes maintenance of complex queries easy. The query can be divided into separate, simple, logical building blocks which can be then used to build more complex CTEs until final result set is generated. CTE can be defined in functions, stored procedures, triggers or even views. After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT, UPDATE or DELETE Data.

What are synonyms? Synonyms give you the ability to provide alternate names for database objects. You can alias object names; for example, using the Employee table as Emp. You can also shorten names. This is especially useful when dealing with three and four part names; for example, shortening server.database.owner.object to object. What is use of EXCEPT Clause? EXCEPT clause is similar to MINUS operation in Oracle.The EXCEPT query and MINUS query returns all rows in the first query that are not returned in the second query. Each SQL statement within the EXCEPT query and MINUS query must have the same number of fields in the result sets with similar data types. How to rebuild Master Database? Master database is system database and it contains information about running server's configuration. When SQL Server 2005 is installed it usually creates master, model, msdb, tempdb resource and distribution system database by default. Only Master database is th one which is absolutely must have database. Without Master database SQL Server cannot be started. This is the reason it is extremely important to backup Master database.

To rebuild the Master database, Run Setup.exe, verify, and repair a SQL Server instance, and rebuild the system databases. This procedure is most often used to rebuild the master database for a corrupted installation of SQL Server.

What is a view? What is the WITH CHECK OPTION clause for a view? A view is a virtual table that consists of fields from one or more real tables. Views are often used to join multiple tables or to control access to the underlying tables. The WITH CHECK OPTION for a view prevents data modifications (to the data) that do not confirm to the WHERE clause of the view definition. This allows data to be updated via the view, but only if it belongs in the view. What is Service Broker? Service Broker is a message-queuing technology in SQL Server that allows developers to integrate SQL Server fully into distributed applications. Service Broker is feature which provides facility to SQL Server to send an asynchronous, transactional message. it allows a database to send a message to another database without waiting for the response, so the application will continue to function if the remote database is temporarily unavailable. Where SQL server user names and passwords are stored in SQL server? They get stored in System Catalog Views sys.server_principals and sys.sql_logins. What are Sparse Columns? A sparse column is another tool used to reduce the amount of physical storage used in a database. They are the ordinary columns that have an optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve nonnull values. What is Filtered Index? Filtered Index is used to index a portion of rows in a table that means it applies filter on INDEX which improves query performance, reduce index maintenance costs, and reduce index storage costs compared with full-table indexes. When we see an Index created with some where clause then that is actually a FILTERED INDEX.

How internal structure of Quorum? Why is means by MSDTC and It is mandatory for sql server 2005 and 2008?

If you have a clustered SQL Server instance, with SSIS or Notification Services or Workstation components the definitive answer is YES, you need to install MSDTC. And it has to be clustered

What means by prepared owner and possible owner ?

Setting up SQL Server 2008 failover cluster on Windows Server 2008 R2 Created cluster (added 2 nodes Node1, Node2). Installed SQL Server 2008 on Node 1. It installed successfully and service came up. I am able to connect through management tool using virtual IP. Now when I want to add Node 2, instance name is not displayed during installation. When I further analyzed, I could see possible owners for both resources SQL Server, SQL Server Agent are just Node 1. Whereas other resources have both Node 1 and Node 2.

Is it possible we run DBCC Checkdb command if database is in suspect mode? Yes.( Take your database into emergency mode then trigger checkdb.) Max .Row size of row in a table. 8086 bytes per row

What the different Topologies in which Replication can be configured? Replication can be configured in any topology depending keeping in view of the complexity and the workload of the entire Replication. It can be any of the following: Publisher, Distributor and Subscriber on the same SQL Instance. Publisher and Distributor on the same SQL Instance and Subscriber on a separate Instance. Publisher, Distributor and Subscriber on individual SQL Instances.

What are the different types of database compression introduced in SQL Server 2008? Row compression and Page compression.

What is Transparent Data Encryption? Introduced in SQL Server 2008 Transparent Data Encryption (TDE) is a mechanism through which you can protect the SQL Server Database files from unauthorized access through encryption. Also, TDE can protect the database backups of the instance on which TDE was setup.

What is the difference between the 2 operating modes of Database Mirroring (mentioned in above answer)? High-Safety Mode is to ensure that the Principal and Mirrored database are synchronized state, that is the transactions are committed at the same time on both servers to ensure consistency, but there is/might be a time lag. High-Performance Mode is to ensure that the Principal database run faster, by not waiting for the Mirrored database to commit the transactions. There is a slight chance of data loss and also the

Mirrored database can be lagging behind (in terms being up to date with Principal database) if there is a heavy load on the Mirrored Server. How to troubleshoot a poor performing query? Using SQL Server Profiler,Using Extended Events,using execution plan.

Top 20 exciting features of SQL Server 2012 Part 1


By Shivprasad koirala, 14 Mar 2013 4.90 (47 votes) Introduction Feature number 1 (Revolution):- Column store indexes Feature number 2 (Evolution):- Sequence objects Feature number 3 (Revolution):- Pagination Feature number 4 (Revolution):- Contained database Feature number 5 (Evolution):- Error handling Which are the next 5 features? Rate this: vote 1vote 2vote 3vote 4vote

Introduction
SQL Server 2012 was released on April 2012 and it has started becoming favorite among professionals. Any new product comes from Microsoft the first thing I personally ask myself, is it worth to jump in?. Is it worth to spend customers hard earned money to get in to that product?. The way to assess the same is dividing the product features in to revolution and evolution. Revolution means its completely a new thing while evolution means there was something already and it has been improvised. In this article I will discuss 20 features of SQL Server 2012 which I personally like (we can agree to disagree on my list) and from these 20 features, 7 are revolution and 13 are evolution. This article is divided in to 4 parts, in every part we will discuss 5 features. So lets start with the first 5 top features.

Feature number 1 (Revolution):- Column store indexes


Column store indexes are unexpected and awesome feature. When I read this feature first time I was like, mouth wide open. You can get this feature when you right click on the indexes folder as Non-Clustered Column store Index , as shown in the below figure.

So lets quickly understand what exactly it does. Now Relational database store data row wise. These rows are further stored in 8 KB page size. For instance you can see in the below figure we have table with two columns Column1 and Column2. You can see how the data is stored in two pages i.e. page1 and page2. Page1 has two rows and page2 also has two rows. Now if you want to fetch only column1, you have to pull records from two pages i.e. Page1 and Page2, see below for the visuals. As we have to fetch data from two pages its bit performance intensive.

If somehow we can store data column wise we can avoid fetching data from multiple pages. Thats what column store indexes do. When you create a column store index it stores same column data in the same page. You can see from the below visuals, we now need to fetch column1 data only from one page rather than querying multiple pages.

Feature number 2 (Evolution):- Sequence objects


This feature is good to have and I personally feel it just mimics Oracles sequence objects. Looks like its just a good to have feeling, if Oracle has it why not SQL Server. A sequence object generates sequence of unique numeric values as per specifications. Many developers would have now got a thought, we have something similar like this called as Identity columns. But the big

difference is sequence object is independent of a table while identity columns are attached to a table. Below is a simple code to create a sequence object. You can see we have created a sequence object called as MySeq with the following specification:

Starts with value 1. Increments with value 1 Minimum value it should start is with zero. Maximum it will go to 100. No cycle defines that once it reaches 100 it will throw an error. If you want to restart it from 0 you should provide cycle. cache 50 specifies that till 50 the values are already incremented in to cache to reduce IO. If you specify no cache it will make input output on the disk.
Collapse | Copy Code

create sequence MySeq as int start with 1 -- Start with value 1 increment by 1-- Increment with value 1 minvalue 0 -- Minimum value to start is zero maxvalue 100 -- Maximum it can go to 100 no cycle -- Do not go above 100 cache 50 -- Increment 50 values in memory rather than incrementing from IO

To increment the value we need to call the below select statement. This is one more big difference as compared to identity.In identity the values increment when rows are added here we need to make an explicit call.
Collapse | Copy Code

SELECT NEXT VALUE FOR dbo.MySequence AS seq_no;

Feature number 3 (Revolution):- Pagination


There are instances when you want to display large result sets to the end user. The best way to display large result set is to split them i.e. apply pagination. So developers had their own hacky ways of achieving pagination using top, row_number etc. But from SQL Server 2012 onwards we can do pagination by using OFFSET and FETCH commands. For instance lets says we have the following customer table which has 12 records. We would like to split the records in to 6 and 6.

So doing pagination is a two-step process:

First mark the start of the row by using OFFSET command. Second specify how many rows you want to fetch by using FETCH command. You can see in the below code snippet we have used OFFSET to mark the start of row from 0position. A very important note order by clause is compulsory for OFFSET command.
Collapse | Copy Code

select * from tblcustomer order by customercode offset 0 rows start from zero

In the below code snippet we have specified we want to fetch 6 rows from the start 0position specified in the OFFSET.
Collapse | Copy Code

fetch next 6 rows only

Now if you run the above SQL you should see 6 rows.

To fetch the next 6 rows just change your OFFSET position. You can see in the below code snippet I have modified the offset to 6. That means the row start position will from 6.
Collapse | Copy Code

select * from

tblcustomer order by customercode offset 6 rows

fetch next 6 rows only

The above code snippet displays the next 6 records , below is how the output looks.

Feature number 4 (Revolution):- Contained database


This is a great feature for people who have to go through pain of SQL Server database migration again and again. One of the biggest pains in migrating databases is user accounts. SQL Server user resides either in windows ADS or at SQL Server level as SQL Server users. So when we migrate SQL Server database from one server to other server these users have to be recreated again. If you have lots of users you would need one dedicated person sitting creating ones for you. So one of the requirements from easy migration perspective is to create databases which are self-contained. In other words, can we have a database with meta-data information, security information etc with in the database itself. So that when we migrate the database, we migrate everything with it. Theres where Contained database where introduced in SQL Server 2012. Creating contained database is a 3 step process: Step 1: - First thing is to enable contained database at SQL Server instance level. You can do the same by right clicking on the SQL Server instance and setting Enabled Contained Database to true.

You can achieve the same by using the below SQL statements as well.
Collapse | Copy Code

sp_configure 'show advanced options',1 GO RECONFIGURE WITH OVERRIDE GO sp_configure 'contained database authentication', 1 GO RECONFIGURE WITH OVERRIDE GO

Step 2 - The next step is to enable contained database at database level. So when create a new database set Containment type to partial as shown in the below figure.

You can also create database with containment set to partial using the below SQL code.
Collapse | Copy Code

CREATE DATABASE [MyDb] CONTAINMENT = PARTIAL ON PRIMARY ( NAME = N'My', FILENAME = N'C:\My.mdf') LOG ON ( NAME = N'My_log', FILENAME =N'C:\My_log.ldf')

Step 3: - The final thing now is to test if contained database fundamental is working or not. Now we want the user credentials to be part of the database , so we need to create user as SQL User with password.

You can achieve the same by using the below script.


Collapse | Copy Code

CREATE USER MyUser WITH PASSWORD = 'pass@123'; GO

Now if you try to login with the user created, you get an error as shown in the below figure. This proves that the user is not available at SQL Server level.

Now click on options and specify the database name in connect to database , you should be able to login , which proves that user is part of database and not SQL Server

Feature number 5 (Evolution):- Error handling


As a developer I am personally very comfortable with using try/catch/throw syntax structure for error handling in c# or vb.net. Thanks to SQL Server team in 2005 they brought in try/catch structure which is very much compatible the way I as a developer was doing error handling in c#. It was nightmare handling error using IF conditions and @error code before SQL Server 2005. Below is a sample code which shows how try/catch code looks.
Collapse | Copy Code

begin try

declare @n int = 0; set @n = 1/0;

end try

begin catch

print('divide by zero'); RAISERROR ( Divide by zero, 16, 1) ;

end catch

But what still is itching me in the above code is when it comes to propagating errors back to the client I was missing the THROW command. We still need to use RAISEERROR which does the job, but lacks lot of capabilities which THROW has. For example to throw user defined messages you need to make entry in to sys.messages table. Below is how the code with throw looks like.
Collapse | Copy Code

begin try -- The code where error has occurred. end try

begin catch -- throw error to the client Throw; end catch

If you want to throw exception with a user defined message defined you can use the below code. No entry need in the sys.messages table.
Collapse | Copy Code

THROW 49903, 'User define exception.', 1

From SQL Server 2012 onwards use Throw rather than raiseerror , looking at the features of throw looks like sooner or later raiseerror will be deprecated . Below is a comparison table which explains the difference between throw vs raiseerror. RaiseError User & system Can generate user and system Can generate only user exception. exception exception. Sys.Messages You can supply adhoc text does not need You need to make an entry in table an entry in Sys.Messages table. Sys.Messages table. Original exception is propagated to the Original exception is lost to the Original exception. client. client. Throw

Which are the next 5 features?


In the next part we will talk about the below features :http://www.codeproject.com/Articles/561797/Top-20-exciting-features-of-SQL-Server-2012Part-2 Feature number 6 (Evolution):- User defined roles Feature number 7 (Evolution):- Windows server core support Feature number 8 (Revolution):- Tabular Model (SSAS)

Feature number 9 (Revolution):- Power view Feature number 10 (Revolution):- DQS Data quality services

You might also like