Professional Documents
Culture Documents
Information Technology
Superior University
Gujranwala Campus
Group Members:
Introduction:
Computed columns are type of columns in which the values are derived based on
one or more other columns. Hence the data type on the computed column depends
on the result of the derived column values.
Computed columns is a feature which has been there in SQL Server since version
2000. But in my experience, I feel that it has been a feature which has been used
less compared to many other features available, and during discussions and
interviews, this is something which most developers slip or fail to answer.
First, we will consider a case where we need to store details on a table without
the usage of computed columns.
Consider we have a table which contains employee details. We have two columns
to store employee’s first and last names. But we also required to have a column
which we need to store their full name as well by concatenating the first and last
names. So the correct way is to have the third column which contains the full
name and the data needs to be inserted while the employee record is created and
it should be maintained in the case where the details are updated as well.
Otherwise, the data integrity will be lost. (One might debate that the full name
can be built from the business logic code using the first and last names. But for
illustration purposes, we would consider that we are maintaining it using SQL
Server.)
CREATE TABLE dbo.Employee (
Id INT
,FirstName VARCHAR(30)
,LastName VARCHAR(30)
,FullName VARCHAR(61)
)
And when queried, the calculated column returns different values as shown
below:
Note: The above mentioned can be achieved using a default constraint as well. I
have used that example on strictly illustration basis.
You can further read on deterministic and non-deterministic function on the
following Microsoft documentation:
Deterministic and Nondeterministic Functions
Computed column values can be persisted by adding the
keyword PERSISTED when the column is created using T-SQL or by the table
designer in SSMS. We will drop ‘FullName’ column and recreate the column.
PERSISTED
Specifies that the Database Engine will physically store the computed values in
the table, and update the values when any other columns on which the computed
column depends are updated. Marking a computed column as PERSISTED
allows an index to be created on a computed column that is deterministic, but not
precise. For more information, see Indexes on Computed Columns. Any
computed columns used as partitioning columns of a partitioned table must be
explicitly marked PERSISTED. Computed column expression must be
deterministic when PERSISTED is specified.
Implementation limitations
Lack of cost model support in the query optimizer
Computed column definition expansion before optimization starts
An example of an implementation limitation is not being able to create a filtered
index on a computed column (even when persisted). There is not much we can
do about this problem category; we have to use workarounds while we wait for
product improvements to arrive.
The lack of optimizer cost model support means SQL Server assigns a small
fixed cost to scalar computations, regardless of complexity or implementation.
As a consequence, the server often decides to recompute a stored computed
column value instead of reading the persisted or indexed value directly. This is
particularly painful when the computed expression is expensive, for example
when it involves calling a scalar user-defined function.
The problems around definition expansion are a bit more involved, and have
wide-ranging effects.
Dates and times can be the most frustrating data types to work with in SQL
Server. Learn everything you need to know about the way dates work with this
handy blog series and enjoy happier times!
This blog is part of a larger tutorial on SQL Server, which you can
read here. Wise Owl also run SQL courses, as well as training
in SSRS, SSAS and SSIS.
This is what DATEDIFF is really doing when you ask it to give you the difference
between two dates in years.
Clearly, using DATEDIFF to calculate the difference between dates in years isn't
accurate enough for something as potentially sensitive as age. In that case we
need a different approach.
Calculating Age in Days and Dividing the Result
A more accurate, but not perfect, way to calculate age in years is to first work out
the difference in days between two dates and then divide the result by the number
of days in a year. The method is shown in the example below:
This is the most accurate way to calculate an age in years, but it's also the most
complex expression to write. The starting point is to use the first calculation we
demonstrated at the top of the page to calculate the difference in years between
two dates:
The role of the TRY statement is to capture the exception. (Because this process
usually comprises several statements, the term “TRY block” typically is used
instead of “TRY statement.”) If an exception occurs within the TRY block, the
part of the system called the exception handler delivers the exception to the other
part of the program, which will handle the exception. This program part is
denoted by the keyword CATCH and is therefore called the CATCH block.
Exception handling using the TRY and CATCH statements is the common way
that modern programming languages like C# and Java treat errors.
Exception handling with the TRY and CATCH blocks gives a programmer a lot
of benefits, such as:
Exceptions provide a clean way to check for errors without cluttering code
Exceptions provide a mechanism to signal errors directly rather than using
some side effects
Exceptions can be seen by the programmer and checked during the
compilation process
SQL Server 2012 introduces the third statement in relation to handling errors:
THROW. This statement allows you to throw an exception caught in the
exception handling block. Simply stated, the THROW statement is another return
mechanism, which behaves similarly to the already described RAISEERROR
statement.
After the execution of the batch in Example 1, all three statements in the batch
won’t be executed at all, and the output of this example is:
Error handling in SQL Server give us control over Transact-SQL code. For
example when things go wrong we get a chance to do something about it and
possibly make it right again. SQL Server error handling can be as simple as just
logging that something happened or it could be us trying to fix an error. It can
even be translating the error in SQL language because we all know how technical
SQL Server error messages could get making no sense and hard to understand.
Luckily we have a chance to translate those messages into something more
meaningful to pass on to the users, developers, etc.
In this article, we’ll take a closer look at the TRY…CATCH statement: the
syntax, how it looks, how it works and what can be done when an error occurs.
Furthermore, the method will be explained in a SQL Server case using a group of
T-SQL statements/blocks which is basically SQL Server way of handling errors.
This is a very simple, yet structured way of doing it and once you get the hang of
it, can be quite helpful in many cases.
On top of that, there is a RAISERROR function that can be used to generate our
own custom error messages which is a great way to translate confusing error
messages into something a little bit more meaningful that people would
understand.