Tuesday, January 21, 2014

How do I use SQLCMD in SQL Server 2005?

SQLCMD lets you write SQL Commands as well as T-SQL Statements and it is the preferred command-line utility in SQL Server 2005. SQLCMD can also be run within SQL Server Managment Studio and runs in side a query pane by enabling the SQLCMD Mode in SQL Server Management Studio from the Query menu's drop-down list.



One of the requirements for running the SQLCMD from the command line (in a DOS screen) is that the client and the server should be listening on/to the same TCP Port. This can be set using the SQL Server Configuration Manager.

Here is an example fo SQLCMD run from command-line.


Commandline access to the program is perhaps one of the nicest features any program can have and the keyboard wizards love them. They can do most things you want done as long as you understand the switches and when & how to use them. A good wizard does most of the things these days, but cannot beat the command-line.

Did I hear you say, short and sweet?

Friday, January 17, 2014

Moving from SQL Server 2012 to SQL Server 2014

SQL Server 2014 is still in CTP2 and SQL Server 2012 has been around for some time. You could get a SQL Server 2012 Express free of cost but with limited functionality.

You can download SQL Server 2014 CTP2 here:
http://technet.microsoft.com/en-US/evalcenter/dn205290.aspx

While on the above make sure you read all the good things you can do with SQL Server 2014.

You may want to know what you can do with SQL Server 2014 CTP1 that you cannot do with SQL Server 2012 Express. If you are looking at just the tasks that you can accomplish, then compare the two images below (the limited number of tasks is that of SQL Server 2012 Express).

SQL Server 2012 Express


SQL Server 2014 CTP1 (enterprise)

 
Quite a lot of the tasks are new features such as Managing Database Encryption, Deploying to Windows Azure VM, etc.
 

Thursday, January 16, 2014

Window function in SQL Server with an example

Windows functions in SQL Server's T-SQL is not related to the Windows operating system but the kind of more detailed ordering (and all the calculations you can do with the ordered set) than you can get by using the regular Group By clause. Windows functions are obviously much more powerful than what you get by mere grouping and subqueries.

The following windows functions were introduced in SQL Server 2005:
Row_Number
Rank
Dense_Rank
NTILE


These are enhanced in SQL Server 2012 by providing that provide better analytics,

  • Windows Order and Frame clauses
  • Windows Offset functions
  (Lag, Lead, First_Value, Last_value)
  • Windows distribution functions
  (Percent-Rank, Cume_Dist, Percentile_Disc and Percentile_Cont)

Conceptually it is as if you have created a window of rows to which you apply some processing and the result is returned in a single row as related to the windowed rows). You define the window in which the processing takes place by the clause OVER.

The following example is using the Orders and Order Details table in Northwind database. If you need a copy of Northwind follow this link:

The following query when run to get raw data,

Get raw filtered data

[Order Details].OrderID, Cast((Orders.OrderDate) as nvarchar(12)) as 'Order Date', [Order Details].Quantity
FROM            [Order Details] INNER JOIN
                         Orders ON [Order Details].OrderID = Orders.OrderID and [Order Details].OrderID <=10253

Returns the following:

OrderID     Order Date   Quantity
----------- ------------ --------
10248       Jul  4 1996  12
10248       Jul  4 1996  10
10248       Jul  4 1996  5
10249       Jul  5 1996  9
10249       Jul  5 1996  40
10250       Jul  8 1996  10
10250       Jul  8 1996  35
10250       Jul  8 1996  15
10251       Jul  8 1996  6
10251       Jul  8 1996  15
10251       Jul  8 1996  20
10252       Jul  9 1996  40
10252       Jul  9 1996  25
10252       Jul  9 1996  40
10253       Jul 10 1996  20
10253       Jul 10 1996  42
10253       Jul 10 1996  40

(17 row(s) affected)

Using Group BY clause:
There are groups (17 rows are in 6 groups) and for these groups we can find the sum of quantity orderd like in,

Select   [Order Details].OrderID, SUM([Order Details].Quantity) as 'QUAN'
FROM            [Order Details] INNER JOIN
                         Orders ON [Order Details].OrderID = Orders.OrderID and [Order Details].OrderID <=10253
                         group by  [Order Details].OrderID

the rows returned are,
OrderID     QUAN
----------- -----------
10248       27
10249       49
10250       60
10251       41
10252       105
10253       102

(6 row(s) affected)

Using the Rank() function

Now we can rank the value Quan and show they are ranked using the Rank() function as in the following:

Select   [Order Details].OrderID,
         SUM([Order Details].Quantity) as 'QUAN',
        RANK() OVER (ORDER BY SUM([Order Details].Quantity)) as RankQuan
FROM            [Order Details] INNER JOIN
                         Orders ON [Order Details].OrderID = Orders.OrderID and [Order Details].OrderID <=10253
                         group by  [Order Details].OrderID

We have a new column produced by,
 RANK() OVER (ORDER BY SUM([Order Details].Quantity)) as RankQuan
which really shows how the rank is evaluated providing a rank for each row.

The response to the above is,

OrderID     QUAN        RankQuan
----------- ----------- --------------------
10248       27          1
10251       41          2
10249       49          3
10250       60          4
10253       102         5
10252       105         6

(6 row(s) affected)

As you notice the Rank() function took care of ordering as well in addition to ranking them.



Use Rank() function one more time:

Let us modify the query further
(and answer the questions whether one can use another Rank() function.

Select   [Order Details].OrderID,
         SUM([Order Details].Quantity) as 'QUAN',
        RANK() OVER (ORDER BY SUM([Order Details].Quantity)) as RankQuan,
        Rank() OVER (ORDER BY [Order Details].OrderID) as 'OrdIDRank'
FROM       [Order Details]        INNER JOIN
                         Orders ON [Order Details].OrderID = Orders.OrderID and [Order Details].OrderID <=10253
                         group by  [Order Details].OrderID

The response is as follows:
OrderID     QUAN        RankQuan             OrdIDRank
----------- ----------- -------------------- --------------------
10248       27          1                    1
10249       49          3                    2
10250       60          4                    3
10251       41          2                    4
10252       105         6                    5
10253       102         5                    6

(6 row(s) affected)

You can see that this has not affected the RankQuan except that the ordering in the final rank gets ordered consequitively.





Sunday, January 5, 2014

What is a SQL Server Managment Studio project and how to create one?

SQL Server Management Studio provides a means to group together all your codes in one place as part of a SQL Server Project. A solution is a container for a project(s) containing all logically related scripts and files in one place. It is available even in SQL Server 2012 Express. This is the platform to use if you want to develop scripts for Database Engine and Analysis Services.

A solution explorer is a pane in SQL Server Management Studio with project containers for managing scripts and files. The concept of Solutions and Projects in Visual Studio is too well known requiring elaboration and this is carried over to SSMS and in fact SQL Server is tightly coupled with Visual Studio. In Visual Studio you can carry a lotsof things that you do in SSMS

How to display Solution Explorer in SQL Server Management Studio?

  • Click File | New | Project... in SSMS to open the New Project window of Visual Studio as shown.


  •  If you choose SQL Server Management Studio Solution then the above interface adds a Blank Solution to which you may add projects. On the other hand
    If you choose SQL Server Management Studio projects then you have the option of adding a SQL Server scripts project or a Analysis Services scripts project as shown.


  • When you accept the option of creating a SQL Serverscripts project Visual Studio creates a Solution and a project within the solution as shown. The Project has folders for Connections, Queries and Miscellaneous scripts.  The default location of the project is at (generally at the logged in Users folder): 
c:\users\mysorian\documents\sql server management studio\Dec2013\Dec2013\Dec2013.ssmssqlproj 


The properties of the solutions shows where the source code is located.


  • From the Solution node you can create a new project or add an existing project to it.

    From the project node (right click) you can add New Item, or an existing item, or a new query, or a new connection.
When you click add New Item, the VS interface is displayed as shown,

  • You can start off with any of the templates to create a query. 

You can open the same project using File | Open |Project/Solution... in SSMS as shown here.


This displays the  Solution explorer as shown by accessing the Source saved on SSMS as shown.


Now you will be developing the project in SQL Server Management Studio.

You can do it both ways or in mixed mode.

Saturday, January 4, 2014

Connecting to SQL Server Express 2012 from Power Pivot - 2

Before you start using Power Pivot you need to install this add-in.

Determine which version (x32 bit or x64bit) Excel you have on your computer. You can find this in File | Help as shown.


You then download the appropriate version of Power Pivot add-in. Also review the following thread here:
http://social.technet.microsoft.com/Search/en-US/Technet?query=Power%20BI%20and%20Jayaram%20krishnaswamy&beta=0&ac=5

Download the add-in from here:
Microsoft® SQL Server® 2012 SP1 PowerPivot for Microsoft Excel® 2010 

This is the power of this add-in in Microsoft's own language:
"Microsoft PowerPivot for Microsoft Excel 2010 is a data analysis tool that delivers unmatched computational power directly within the software users already know and love — Microsoft Excel."
 For the 32bit Office, the PowerPivot_for_Excel_x86.msi (98.5MB) was downloaded and installed.

The following are some of the screen shots for connecting to SQL Server 2012 Express:

Launch PowerPivot window from MS Excel as shown by clicking on PowerPivot
 Window (extreme left)


Click from drop-downs From DataSources followed by From SQL Server in the PowerPivot for Excel window.

Table Connect Wizard gets displayed requesting SQL Server  details as shown.


Provide the database you want to connect to as shown. You better have permissions to do so.

I

If want you can test (and troubleshoot) connectivity from this screen.

Click Next. You can import a set of tables or design data using query as shown in the screen that gets displayed.


The following windows is displayed for the first option and it shows few tables selected. The second part (right-side) of the image shows the Preview and if needed some filtering that can be made.


The import was successful as shown here after some processing.

 
Click Close and the data gets into the PowerPivot window as shown.


You can see relationships that exists as shown.

Now begins the hard work of things that you want to do with this data.

Good luck


Friday, January 3, 2014

Connecting to SQL Server 2012 Express from Power Pivot - 1

Previously we have seen connecting to a database from Power Query. Microsoft also has Power Pivot which also can access SQL Server 2012 to model a data source. In what way is Power Query different from Power Pivot?

There is a functionality difference between the two. Power Query is more like SQL Server Integration Services while Power Pivot is akin to SQL Server Analysis Services. Even a cursory look at the
'ribbon' would reveal this as shown:


In Power Query UI you do not see analysis related controls.

As to connecting to data you do see there is some overlap. However, Power Query can access lot more types of data sources than Power Pivot.  These are data sources that Power Query can access,


The following image shows data sources that Power Pivot can bring in,

 Also, review the user interface differences between the two tools, one provides more analytical support than the other,

However both Power Query as well as Power Pivot can connect to SQL Server 2012 Express by clicking on the From Database drop-down.

Go to Part 2 here:



Wednesday, January 1, 2014

When Identity Security Becomes a Wall — Not a Shield

After a breach that forced a reset of my digital identity, I hit a roadblock I never anticipated: multi-factor authentication (2FA) locked m...