SQL SERVER – Server Type and File Extention

Owing to my personal experience so far, I can undeniably say that Microsoft Windows products are outstanding. One of the reasons that make them exceptional is their little nifty tricks. For instance, every time I double click myfilename.sql it opens Microsoft SQL Server Management Studio (SSMS). The reason how Windows discerns that it has to open SSMS is because the extension of file I had clicked is .sql. I explored and found that SQL Server has few more filetypes associated with it, which are as follows.

SQL Server – .sql
SQL Server Compact 3.5 SP1 – .sqlce
SQL Server Analysis Service – .mdx, .xmla

Moving on, I would like to talk about another command that can open SQL Server Management Studio. Just type sqlwb in command prompt to open SSMS and bring credential prompt on screen.

I hope you find this article useful. Let me have your feedback so that we can take this topic further.

Reference : Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Logical Query Processing Phases – Order of Statement Execution

You can download the poster from Itzik Ben-Gan’s T-SQL Querying page over here.

Of late, I penned down an article – SQL SERVER – Interesting Observation of ON Clause on LEFT JOIN – How ON Clause Effects Resultset in LEFT JOIN – which received a very intriguing comment from one of my regular blog readers Craig. According to him this phenomenon happens due to Logical Query Processing. His comment instigated a question in my mind. I have put forth this question to all my readers at the end of the article. Let me first give you an introduction to Logical Query Processing Phase.

What actually sets SQL Server apart from other programming languages is the way SQL Server processes its code. Generally, most programming languages process statement from top to bottom. By contrast, SQL Server processes them in a unique order which is known as Logical Query Processing Phase. These phases generate a series of virtual tables with each virtual table feeding into the next phase (virtual tables not viewable). These phases and their orders are given as follows:

1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10 ORDER BY
11. TOP

As OUTER join is applied subsequent to ON clause, all rows eliminated by the ON clause will still be included by the OUTER join as described in the article SQL SERVER – Interesting Observation of ON Clause on LEFT JOIN – How ON Clause Effects Resultset in LEFT JOIN.

However, I am perplexed about the last two, ORDER BY and TOP. According to some people TOP comes first in logical query processing phase while others suggest that ORDER BY comes first. Now, here I’ve laid down my questions for you all to think about:

1) What is the correct answer for order query processing phase – ORDER BY or TOP?
2) How can we create an example to verify query processing phase for ORDER BY and TOP?

I will soon publish the answers I receive to the above questions on this blog, with due credit given to my readers.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQLAuthority News – Top 10 Strategic Technologies for 2009

Gartner, Inc. analysts highlighted the top 10 technologies and trends that will be strategic for most organizations. Factors that denote significant impact include a high potential for disruption to IT or the business, the need for a major dollar investment, or the risk of being late to adopt.

The top 10 strategic technologies for 2009 include:

Virtualization. Much of the current buzz is focused on server virtualization, but virtualization in storage and client devices is also moving rapidly.

Cloud Computing. Cloud computing is a style of computing that characterizes a model in which providers deliver a variety of IT-enabled capabilities to consumers

ServersBeyond Blades. Servers are evolving beyond the blade server stage that exists today. This evolution will simplify the provisioning of capacity to meet growing needs.

Web-Oriented Architectures. The Internet is arguably the best example of an agile, interoperable and scalable service-oriented environment in existence.

EnterpriseMashups. Enterprises are now investigating taking mashups from cool Web hobby to enterprise-class systems to augment their models for delivering and managing applications.

Specialized Systems. Appliances have been used to accomplish IT purposes, but only with a few classes of function have appliances prevailed.

Social Software and Social Networking. Social software includes a broad range of technologies, such as social networking, social collaboration, social media and social validation.

Unified Communications. During the next five years, the number of different communications vendors with which a typical organization works with will be reduced by at least 50 percent.

Business Intelligence. Business Intelligence (BI) have a direct positive impact on a company’s business performance, dramatically improving its ability to accomplish its mission by making smarter decisions at every level of the business from corporate strategy to operational processes.

Green IT. Shifting to more efficient products and approaches can allow for more equipment to fit within an energy footprint, or to fit into a previously filled center.

Reference : Pinal Dave (http://blog.sqlauthority.com), Gartner Inc.(Published after receiving permission)

SQL SERVER – Interesting Interview Questions – Revisited

I really enjoyed users participation in my previous question. Read SQL SERVER – Interesting Interview Questions before continuing reading this article. This interview question was about user participation and about how good and how different you can come with your T-SQL script. What I really liked is that many users took this test seriously and did their best to answer. I really want to congratulate all the readers who have attempted to answer this question.

As I have said earlier it did not matter what is the database structure, but it mattered what should be the good database architecture design. Here it only mattered if you can write T-SQL based on question.

Following readers have got correct answer.

Zod

Eric

Imran Mohammed

bhadeliaimran

fly

pom

I really want to thank and congratulate readers who have answered this question correct. I would suggest that you book mark this article as well original article for your personal reference. In future if you need ever recommendation regarding any interview I will be happy to list this articles for you in reference.

Now let us see their solution in detail.Please go over interview question again before continuing reading following comments.

Solution byZod

DECLARE @Class1 INT, @Class2 INT, @Class3 INT,
@Class1Total INT, @Class2Total INT, @Class3Total INT
SET
@Class1Total = 0
SET @Class2Total = 0
SET @Class3Total = 0
DECLARE student_CURSOR CURSOR FOR
SELECT
Class1, Class2, Class3
FROM StudentsEnroll
OPEN student_CURSOR
FETCH next FROM student_CURSOR
INTO @Class1, @Class2, @Class3
WHILE @@FETCH_STATUS = 0
BEGIN
SET
@Class1Total = @Class1Total + @Class1
SET @Class2Total = @Class2Total + @Class2
SET @Class3Total = @Class3Total + @Class3
FETCH next FROM student_CURSOR
INTO @Class1, @Class2, @Class3
END
CLOSE
student_CURSOR
DEALLOCATE student_CURSOR
SELECT 'Class1 has ' + CAST(@Class1Total AS VARCHAR(10)) + ' students'
UNION ALL
SELECT 'Class2 has ' + CAST(@Class2Total AS VARCHAR(10)) + ' students'
UNION ALL
SELECT 'Class3 has ' + CAST(@Class3Total AS VARCHAR(10)) + ' students'
GO

Solution by Eric

SELECT
'Class1 has ' + CAST(SUM(CAST([Class1] AS INT)) AS VARCHAR(10)) + ' students.\n'
+ 'Class2 has ' + CAST(SUM(CAST([Class2] AS INT)) AS VARCHAR(10)) + ' students.\n'
+ 'Class3 has ' + CAST(SUM(CAST([Class3] AS INT)) AS VARCHAR(10)) + ' students.\n'
FROM [StudentsEnroll];

Solution by Imran Mohammed

SELECT CLASS+ ' has '+CONVERT(VARCHAR(10),COUNT(orders ))+' Students ' 'Output' FROM (
SELECT students, Class, Orders
FROM
(SELECT students, class1, class2,class3
FROM studentsenroll ) p
UNPIVOT
(Orders FOR Class IN
(class1, class2, class3 )
)
AS unpvt) X WHERE orders = 1 GROUP BY class

Solution by bhadeliaimran

DECLARE @opXml AS XML
SET @opXml = '<ClassStud value=''' + REPLACE(
(
SELECT
'Class1 has ' + CAST(SUM(CAST(Class1 AS SMALLINT)) AS VARCHAR(4)) + ' students' ,
',Class2 has ' + CAST(SUM(CAST(Class2 AS SMALLINT)) AS VARCHAR(4)) + ' students' ,
',Class3 has ' + CAST(SUM(CAST(Class3 AS SMALLINT)) AS VARCHAR(4)) + ' students'
FROM [StudentsEnroll]
FOR XML PATH('') ), ',', ''' /><ClassStud value=''') + ''' />'
SELECT x.value('@value', 'varchar(100)') AS [output]
FROM @opXml.nodes('/ClassStud') AS p(x)

Solution by fly

DECLARE @tbl TABLE (i INT)
INSERT INTO @tbl (i)
SELECT 1
UNION
SELECT
2
UNION
SELECT
3
SELECT
CASE WHEN i = 1 THEN 'Class 1 has ' + CAST(SUM(CAST (class1 AS INT)) AS VARCHAR(100)) + ' Students' ELSE
CASE WHEN i = 2 THEN 'Class 2 has ' + CAST(SUM(CAST (class2 AS INT)) AS VARCHAR(100)) + ' Students' ELSE
CASE WHEN i = 3 THEN 'Class 3 has ' + CAST(SUM(CAST (class3 AS INT)) AS VARCHAR(100)) + ' Students' ELSE '' END
END
END
FROM
[StudentsEnroll]
JOIN @tbl t ON i <= 3
GROUP BY i

Solution by pom

SELECT 'class1 has ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class1=1 GROUP BY class1) AS CHAR(3)) + 'Students'
UNION
SELECT
'class2 has ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class2=1 GROUP BY class2) AS CHAR(3)) + 'Students'
UNION
SELECT
'class3 has ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class3=1 GROUP BY class3) AS CHAR(3)) + 'Students'

Other two solution proposed earlier in original article.

Solution 1 – Using only SELECT statement

DECLARE @Col INT
SET
@Col = 1
WHILE (@Col < 4)
BEGIN
EXEC
(‘SELECT     ”Class’+@Col+‘ Has ” + CAST(COUNT(Students) AS VARCHAR(100)) + ” Students” Results
FROM         studentsenroll
WHERE         Class’
+@Col+‘ = 1
GROUP BY      Class’
+@Col)
SET @Col = @Col + 1
CONTINUE
END

Solution 2 – Getting results in one resultset

SELECT ‘Class1 has ’ + CAST(COUNT(*) AS VARCHAR(10)) +‘ Students’
FROM StudentsEnroll
WHERE Class1 = 1
GROUP BY Class1
UNION ALL
SELECT ‘Class2 has ’ + CAST(COUNT(*) AS VARCHAR(10)) +‘ Students’
FROM StudentsEnroll
WHERE Class2 = 1
GROUP BY Class2
UNION ALL
SELECT ‘Class3 has ’ + CAST(COUNT(*) AS VARCHAR(10)) +‘ Students’
FROM StudentsEnroll
WHERE Class3 = 1
GROUP BY Class3

Now the real question is which of this articles are favorite solution of yours. Please leave your choice along with reason in comment.

I promise if you can not write solution but if you understand the proposed solution in this article you will usually pass any database T-SQL problem in interviews. Additionally, if you are looking for job in SQL Server related area please find your right job here. Make sure to read all the interview questions and answers for SQL Server 2008 before you appear for interview.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQL SERVER 2008 – Top 10 SQL Server 2008 Features for Independent Software Vendor Applications

Top 10 SQL Server 2008 Features for ISV Applications
Author: Burzin Patel
Contributor: Kun Cheng
Reviewers: Sanjay Mishra, Denny Lee, Mike Ruthruff, Sharon Bjeletich, Mark Souza, Peter Carlin, Hermann Daeubler, Peter Scharlock, Wanda He

Microsoft SQL Server 2008 has hundreds of new and improved features, many of which are specifically designed for large scale independent software vendor (ISV) applications, which need to leverage the power of the underlying database while keeping their code database agnostic. This article presents details of the top 10 features that we believe are most applicable to such applications based on our work with strategic ISV partners. Along with the description of each feature, the main pain-points the feature helps resolve and some of the important limitations that need to be considered are also presented.

1 Data Compression
2 Backup Compression
3 Transparent Data Encryption
4 Data Collector and Management Data Warehouse
5 Lock Escalation
6 Plan Freezing
7 Optimize for Ad hoc Workloads Option
8 Resource Governor
9 Table-Valued Parameters
10 Filestream

Read complete article here.

Abstract courtesy : Microsoft

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQLAuthority News – SQL Server White Paper: SQL Server 2008 Compliance Guide

Note: Download White Paper by Microsoft

Organizations across the globe are being inundated with regulatory requirements. They also have a strong need to better manage their IT systems to ensure they are operating efficiently and staying secure. Microsoft is often asked to provide guidance and technology to assist organizations struggling with compliance. The SQL Server 2008 Compliance Guidance white paper was written to help organizations and individuals understand how to use the features of the Microsoft SQL Server 2008 database software to address their compliance needs. This paper serves as an accompaniment to the SQL Server 2008 compliance software development kit (SDK), which provides sample code and guidance for understanding SQL Server 2008 compliance features and using them for developing solutions.

Download White Paper

Abstract courtesy : Microsoft

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQLAuthority News – TOP Downloads – Bookmark

Recently I have got many many request for SQL Server Interview Questions and Answers as well related articles. It seems many people are looking for Job or appearing for interview at this time of the year. I have included list of the my top downloads in side bar of the blog, still I receive many curious questions as side bar does not show up in RSS feed.

Please book mark this post for future reference.

SQL Server 2008 Interview Questions and Answers Download
SQL Server Management Studio Keyboard Shortcuts (SSMS Shortcuts) Download
SQL Server 2008 Certification Path Complete Download
SQL Server Cheat Sheet Download
SQL Server Database Coding Standards and Guidelines Complete List Download

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQL SERVER – Introduction to CLR – Simple Example of CLR Stored Procedure

CLR is abbreviation of Common Language Runtime. In SQL Server 2005 and later version of it database objects can be created which are created in CLR. Stored Procedures, Functions, Triggers can be coded in CLR. CLR is faster than T-SQL in many cases. CLR is mainly used to accomplish task which are not possible by T-SQL or can use lots of resources. CLR can be usually implemented where there is intense string operation, thread management or iteration methods which can be complicated for T-SQL. Implementing CLR provides more security to Extended Stored Procedure.

Let us create one very simple CLR where we will print current system datetime.

1) Open Microsoft Visual Studio >> Click New Project >> Select Visual C# >> Database >> SQL Server Project

2) Either choose from existing database connection as reference or click on Add New Reference. In my example I have selected Add New Reference.

3) If you have selected existing reference skip to next step or add database reference as displayed in image.

4) Once database reference is added following project will be displayed in Solution Explorer. Right click on Solution Explorer >> Click on Add >> Stored Procedure.

5) Add new stored procedure template from following screen.

6) Once template added it will look like following image.

7) Now where it suggest to //Put your code here. Replace it with code displayed in the image. Once the code is complete do following two steps.
a) Click on menu bar >> Build >> Build ProjectName
b) Click on menu bar >> Build >> Deploy ProjectName
Building and Deploying project should give successful message.


using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void CLRSPTest()
{
SqlPipe sp;
sp = SqlContext.Pipe;
String strCurrentTime = “Current System DateTime is: “
+ System.DateTime.Now.ToString();
sp.Send(strCurrentTime);
}
};
8) Now open SQL Server Management Studio and run following script in Query Editor. It should return current system datetime. Running it again the time will change.

USE AdventureWorks
GO
EXEC dbo.CLRSPTest
GO

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQL SERVER – Downgrade Database to Previous Version

Today I am writing on the topic which I do not like to write much. I enjoy writing usually positive or affirmative posts. Recently I got email from two different DBA where they upgraded to SQL Server 2005 trial version on their production server and now as their trial version was expire they wanted to downgrade their database to previous licensed version they had.

The main questions is how they can downgrade the from SQL Server 2005 to SQL Server 2000?

Answer is : Not Possible.

There are no tools or native SQL Server facility which does this. I am also not aware of any of the tool available from third party software vendors. Database (.mdf, .ndf, .ldf) can not be restored from higher version to lower versions. If you want to downgrade your server, fresh install of previous version of server should be done first. Right after that recreate all the schema to create empty tables and database objects (SP, UDFs etc). Use SSIS/DTS or any other import/export utility and import data from upgraded version to downgraded version.

First of all I am true believer of licensed software. I do not like to use or encourage use of pirated software. Again, I do not like to even downgrade to lower version. SQL Server 2008 and SQL Server 2005 have many new and advance features over SQL Server 2000. One must keep on moving towards new technology and upgrade themselves and company technology.

It is responsibility of the each developer to encourage technological advancement and prevent piracy.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

SQLAuthority News – SQL Server 2008 – Microsoft Certifications for 70-432 70-433 70-450 70-452

I have received many emails requesting information about SQL Server certifications examples. Microsoft has released new set of exams for SQL Server 2008 certifications. I am listing them here for quick reference.

Exam 70-432 – TS: Microsoft SQL Server 2008, Implementation and Maintenance

Installing and Configuring SQL Server 2008 (10 percent)
Maintaining SQL Server Instances (13 percent)
Managing SQL Server Security (15 percent)
Maintaining a SQL Server Database (16 percent)
Performing Data Management Tasks (14 percent)
Monitoring and Troubleshooting SQL Server (13 percent)
Optimizing SQL Server Performance (10 percent)
Implementing High Availability (9 percent)

—————————————

Exam 70-433 – TS: Microsoft SQL Server 2008, Database Development

Implementing Tables and Views (14 percent)
Implementing Programming Objects (16 percent)
Working with Query Fundamentals (21 percent)
Applying Additional Query Techniques (15 percent)
Working with Additional SQL Server Components (11 percent)
Working with XML Data (12 percent)
Gathering Performance Information (11 percent)

—————————————

Exam 70-450 – PRO: Designing, Optimizing, and Maintaining a Database Administrative Solution Using Microsoft SQL Server 2008

Designing a SQL Server Instance and a Database Solution (14 percent)
Designing a Database Server Security Solution (15 percent)
Designing a Database Solution for High Availability (15 percent)
Designing a Backup and Recovery Solution (20 percent)
Designing a Monitoring Strategy (13 percent)
Designing a Strategy to Maintain and Manage Databases (14 percent)
Designing a Strategy for Data Distribution (9 percent)

—————————————

Exam 70-452 – PRO: Designing a Business Intelligence Infrastructure Using Microsoft SQL Server 2008

Designing and Managing Reports (20 percent)
Designing Data Mining Models (10 percent)
Administering a BI Solution (15 percent)
Designing the BI Architecture (21 percent)
Designing and Deploying SSIS Packages (16 percent)
Designing an Analysis Services Database (18 percent)

Reference : Pinal Dave (http://blog.SQLAuthority.com)