Thursday, April 28, 2016

Common Table Expression (CTE) simply explained

Microsoft documentation defines Common Table Expression thus,

"A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.

It is unlike a temporary table (persisted) during a connection or a view persisted in the database. CTE is only in memory.

We will consider CTE's many use case scenarios in later posts and the objective of this post is to give a simple example using the Northwind database.

A CTE should have the following:
  • CTE with a CTE Expression name
  • An Optional Column list
  • A query defining the CTE

After a CTE is defined it can be referenced like a table or view and you can do all of the following statements:

  • Select
  • Insert
  • Update
  • Delete
It can also be used in defining a view by providing the columns.

Now let me define CTE using the Employees table in Northwind. The example is just to show the syntax as the result can be obtained from a single query without using CTE.

USE Northwind
Go
---define the common table expression
WITH NEmp_CTE (FirstName,LastName,CITY)  /*Name: Nemp_CTE, Column List:FirstName, LastName,City*/
AS
---define the CTE query
(SELECT FirstName,LastName,City
from Employees
where City in ('Tacoma','London')
)
---Use it in a Select Query
SELECT LastName,FirstName,CITY
From NEmp_CTE
where LastName <>'Buchanan'
go

The response to this query is:

Note: The response such as the above does not really need a Common Table Expression but the point was to show the construction of the Syntax used for CTE. In fact, it is to make complex queries more readable.

Wednesday, April 27, 2016

Download SQL Server 2016 Release Candidate 3 and evaluate


SQL2K16RC3.png
You can download the 120 day edition from here after registering.

Three types of download (only 64-bit):

ISO
CAB
Azure

Hardware and software requirements (abbreviated):

  • .NET Framework 4.6, Installation setup installs.NET Framework
  • Network Software: Shared memory, Named Pipes, TCP/IP and VIA
  • Hard disk space: Minimum of 6GB
  • Drive/Monitor/Internet required.
  • Memory: 1GB
  • Processor: x64 bit 1.4GHZ
  • Processor type: x64 Processor: AMD Opteron, AMD Athlon, Intel Xeon with Intel EM64T Support and Intel Premium with EM64T support
  • No support for WOW 64

Here are the benefits of SQL Server 2016:
Enhanced in-memory performance provide up to 30x faster transactions, more than 100x faster queries than disk based relational databases and real-time operational analytics
◾New Always Encrypted technology helps protect your data at rest and in motion, on-premises and in the cloud, with master keys sitting with the application, without application changes
◾Built-in advanced analytics– provide the scalability and performance benefits of building and running your advanced analytics algorithms directly in the core SQL Server transactional database
◾Business insights through rich visualizations on mobile devices with native apps for Windows, iOS and Android
◾Simplify management of relational and non-relational data with ability to query both through standard T-SQL using PolyBase technology
◾Stretch Database technology keeps more of your customer’s historical data at your fingertips by transparently stretching your warm and cold OLTP data to Microsoft Azure in a secure manner  without application changes
◾Faster hybrid backups, high availability and disaster recovery scenarios to backup and restore your on-premises databases to Microsoft Azure and place your SQL Server AlwaysOn secondaries in Azure

There are several different types of SQL Server 2014 available and detials are here:
https://msdn.microsoft.com/en-us/library/ms143506(v=sql.130).aspx#hwswr

Tuesday, April 26, 2016

How do you explain this correlated subquery?

This post is to explain the processing of a correlated subquery as this type of subquery is  not so straight forward.

Non-correlated subquery is easy to understand as the inner query is evaluated first and then the outer query is evaluated using the value(s) returned by the inner query.

In the case of correlated subquery, the outer query is run first and then the inner query is run for each value of outer query to find the match for the value returned.

This example is chosen to explain the processing of a correlated subquery as the number of outer evaluations is quite small (only 9 employees). This is a query to the Employees table in the Northwind database in SQL Server.

This query is trying to find the employees living in a city from where the orders were also shipped.



This returns the following response:

Of the 9 employees only 6 of the above employees lived in the same city where the orders were shipped.

Now review this tutorial created using Sway to follow the processing:
https://docs.com/jayaram-krishnaswamy/4697/undestanding-the-processing of a correlated

Wednesday, April 20, 2016

Get Microsoft SQL Server Developer Edition for FREE


Not long ago I paid 60+ dollars to buy SQL Server Developer Edition from Microsoft Store. Since end of March 2016, you can get it for free. That's very generous.  It is a free download but you may have to sign-in to Visual Studio Dev Essentials before you can download SQL Server 2014 Developer Edition. It is assumed that you have a licensed Windows 10 or Windows Server.

SQL Server Developer edition has all the capabilities of the Enterprise Edition, however it is only for development and testing. Of course you can do BI on it. You may not run a business with it.

In Microsoft's own language,
"Visual Studio Dev Essentials is Microsoft’s most comprehensive free developer program ever, with everything you need to build and deploy your app on any platform, including state-of-the-art tools, the power of the cloud, training, and support."

This is great stuff. If you are serious you will not let go of this opportunity.

Go this URL to download SQL Server 2014 Developer Edition with Service Pack 1 (x86) or (x64)
https://myprodscussu1.app.vssubscriptions.visualstudio.com/Downloads?pid=1682









More on this news here:
https://blogs.technet.microsoft.com/dataplatforminsider/2016/03/31/microsoft-sql-server-developer-edition-is-now-free/

Thursday, April 14, 2016

Creating a Scalar-valued function in SQL Server 2014

First of all what is a Scalar-valued function?

A scalar function in SQL Server is similar to the generally accepted meaning function, a function is defined by the variables (parameters) with some operation on them and the resulting value is the return value of the function.

  • If x and y are the variables(parameters) than the
    function(x,y)=x+y defines a scalar function that returns a value which is the sum of x and y.
  • The scalar function is not limited to just two parameters and can have more.

Where do you find the Scalar-valued function in the Object Explorer?

You will find the Scalar-valued function node in all the databases in the Programmability node as shown here for AdventureWorks2012 database.

Sway5_01

How do you create a Scalar-Valued Function in SQL Server Management Studio?

Watch this Swaytorial.
https://docs.com/jayaram-krishnaswamy/9721/creating-a-scalar-valued-function-in-sql-server


Friday, March 25, 2016

Scripting a database in SQL Server 2012

You may need to use your database on one server on another server. Perhaps you just want to have a copy to work with to try something. In this case you need to somehow get it on to the other server. The following video created using Microsoft Sway shows the procedure. Just use the navigation keys at the bottom right.

Enjoy this new format of presentation.

Tuesday, March 15, 2016

SQL Server can now run on LINUX


First week of March this year Microsoft announced the availability of SQL Server on LINUX for both on-premises and the cloud. The core RDBS capabilities are now in preview but will be available mid-2017. The above image from Microsoft site.


More details here:
http://blogs.microsoft.com/blog/2016/03/07/announcing-sql-server-on-linux/

All the very best on Windows will be available for LINUX as well such as:
Mission-Critical Performance
Industry-Leading TCO
The best in best security
Plus all the hybrid cloud innovations
These all available out-of-the box.

Microsoft is not just for Windows but is truly cross-platform.
The preview is available as of the announcement.
Sing-up here to get more as the preview is limited to a select initial group:
https://www.microsoft.com/en-us/server-cloud/sql-server-on-linux.aspx

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...