Skip to content

How to Concatenate

Concatenation in SQL Server is done by using the plus (+) operator.  Let’s setup a table and look at an example by concatenating customer’s first names and last names:

-- Create Customers table
CREATE TABLE #Customers
(
	FirstName varchar(50),
	LastName varchar(50)
)

INSERT INTO #Customers
(
	FirstName,
	LastName
)
SELECT 'Vincent', 'Vega'
UNION
SELECT 'Marsellus', 'Wallace'
UNION
SELECT 'Jules', NULL

-- Concatenate FirstName and LastName
SELECT FirstName + LastName
FROM #Customers

Now when we run the output we will see something interesting. When we try to concatenate a null value with a string, we are returned with a NULL value. This shows the important need to use the ISNULL function when concatenating values.

sql concatenate strings

Let’s use the ISNULL function now to replace the NULL with an empty string:

SELECT ISNULL(FirstName, '') + ISNULL(LastName, '')
FROM #Customers

Now we see that the we are returned with a string rather than a NULL.

How to Backup a Table

There is not standard method for backing up a table in SQL Server.  A common request I receive from clients is to only backup specific tables within a database.  Well, unfortunately there is no out of the box method for doing this akin to a database backup so we need to roll our own way.

It is usually done one of two ways.  The first is to use a select into method to copy the table.  The other way is to script the table out using the Generate Scripts Tasks.  Both methods have positives and negatives.  First off, the SELECT.. INTO Method is by far the fastest.  It can copy a large number of rows very quickly, the downfall to this however, is that it does not carry over the Keys, Indexes or Constraints.  The Generate Scripts method is slow (and I don’t recommend it for very large tables), however it can facilitate copying over any of the other objects associated with the table.  Let’s look at the generate scripts task:

First, right click on the database that contains the table you want to backup and choose Tasks -> Generate Scripts.

backup table generate scripts

The Generate Scripts Wizard Appears.  Select “Next” past the splash screen then select the database that contains the table.  The next screen that appears is the Script Options.

backup table script wizard

Table/View Options

Scroll down on the script options until you see Table/View Options.  We want the following to be true: Check Constraints, Script Data, Foreign Keys, Primary Keys, Triggers, & Unique Keys.  (Or you can choose whatever you need of course).  Select Next and we are presented with the Select Object Types Screen.

script_wizard_backup_table

Select Tables and hit next.  Lastly, choose the table or tables you want to backup and hit next.script_wizard_choose_tables

The last dialog provides the medium you want to output the script to.  If you are backing up a large table, I suggest you output to a file.  Good luck!

How to Backup a Database

Backing up a database is relatively straight forward using SQL Server Management Studio.

  1. First open management studio and connect to the Database Server that contains the database you want to backup.
  2. Expand the “Databases” folder and right-click on the database you want to make a backup of.
  3. how to backup database menu

    Tasks -> Backup

  4. Choose Tasks -> Back Up… (now the Back up database dialog appears)
  5. back up database dialog

    From here, if you simply hit the “OK” button, your database will be backed up in the default location. However, let’s specify a location so we can find it.
  6. Select the “Remove” button to remove the current backup location
  7. Select the “Add” button and add a new location to backup the database to. Now choose a location on disk and give it a file name. It is good practice to include the date of the backup in the filename. You may also want to save the file name with a .BAK extension
  8. You can select “OK” and the backup will begin. You can optionally at this point, select the “Script” button at the top of the dialog in order to retrieve the script that SSMS uses to perform the backup. A new window will open and you will have your script. You can then use this script in a job, or run it yourself.

How to Add a Linked Server

Adding a Linked server can be done by either using the GUI interface or the sp_addlinkedserver command.

Adding a linked Server using the GUI

There are two ways to add another SQL Server as a linked server.  Using the first method, you need to specify the actual server name as the “linked server name”.  What this means is that everytime you want to reference the linked server in code, you will use the remote server’s name.  This may not be beneficial because if the linked server’s name changes, then you will have to also change all the code that references the linked server.  I like to avoid this method even though it is easier to initially setup.  The rest of the steps will guide you through setting up a linked server with a custom name:

To add a linked server using SSMS (SQL Server Management Studio), open the server you want to create a link from in object explorer.

  1. In SSMS, Expand Server Objects -> Linked Servers -> (Right click on the Linked Server Folder and select “New Linked Server”)
  2. Add New Linked Server

    Add New Linked Server

  3. The “New Linked Server” Dialog appears.  (see below).
  4. Linked Server Settings

    Linked Server Settings

  5. For “Server Type” make sure “Other Data Source” is selected.  (The SQL Server option will force you to specify the literal SQL Server Name)
  6. Type in a friendly name that describes your linked server (without spaces). I use AccountingServer.
  7. Provider – Select “Microsoft OLE DB Provider for SQL Server”
  8. Product Name – type: SQLSERVER (with no spaces)
  9. Datasource – type the actual server name, and instance name using this convention: SERVERNAME\INSTANCENAME
  10. ProviderString – Blank
  11. Catalog – Optional (If entered use the default database you will be using)
  12. Prior to exiting, continue to the next section (defining security)
Define the Linked Server Security

Linked server security can be defined a few different ways. The different security methods are outlined below.  The first three options are the most common:

Option Name Description
Be made using the login’s current security context Most Secure. Uses integrated authentication, specifically Kerberos delegation to pass the credentials of the current login executing the request to the linked server. The remote server must also have the login defined. This requires establishing Kerberos Constrained Delegation in Active Directory, unless the linked server is another instance on the same Server.  If instance is on the same server and the logins have the appropriate permissions, I recommend this one.
Be made using this security context Less Secure. Uses SQL Server Authentication to log in to the linked server. The credentials are used every time a call is made.
Local server login to remote server login mappings You can specify multiple SQL Server logins to use based upon the context of the user that is making the call.  So if you have George executing a select statement, you can have him execute as a different user’s login when linking to the linked server.  This will allow you to not need to define “George” on the linked server.
Not be made If a mapping is not defined, and/or the local login does not have a mapping, do not connect to the linked server.
Be made without using a security context Connect to the server without any credentials.  I do not see a use for this unless you have security defined as public.
  1. Within the same Dialog on the left menu under “Select a Page”, select Security
  2. Enter the security option of your choice.
  3. Linked Server Security Settings

    Linked Server Security Settings

  4. Click OK, and the new linked server is created

How to Move TempDB

In order to move TempDB, use the alter database command with “modify file” and specify a new path.

use master
go
Alter database tempdb modify file (name = tempdev, filename = 'e:\tempdb\tempdb.mdf')
go
Alter database tempdb modify file (name = templog, filename = 'e:\tempdb\templog.ldf')
go

Make sure the folder path already exist otherwise you will get an error.

Once complete, an informative message will appear stating that tempdb will be moved the next time SQL is started.


The file "tempdev" has been modified in the system catalog. The new path will be used the next time the database is started.
The file "templog" has been modified in the system catalog. The new path will be used the next time the database is started.

This will require a restart of the SQL Server Database Engine service.

What is Parallelism?

Parallelism is a feature in SQL Server which allows expensive queries to utilize more threads in order to complete quicker. The query optimizer makes the determination of how expensive a query is based upon the Cost Threshold for Parallelism setting set at the SQL Server Instance level. This cost threshold for parallelism is an estimate (roughly defined in seconds) that the query optimizer has determined a statement will take based on an estimated (or cached) execution plan. Generally the queries that qualify for parallelism are high IO queries. In a normal process, a source is read using a single SPID (Server Process ID) and it output using the same SPID. In parallelism, multiple SPIDs are used to read a source (this is known as distributing streams), then an operation may be performed in the streams, then the streams are gathered. Below is an illustration of this:

parallelism_diagram

Although parallelism may seem like a good idea, often times it is not. For OLTP systems that facilitate a lot of user requests parallelism is usually an indication of poorly written queries and/or queries that are in need of indexes. It can also cause issues on servers that have a lot of processors that also have disk IO contention. The reason for this is because parallelism will by default spawn as many SPIDs as their are processors. In other words, if you have a rogue query joining a hundred million records that does not qualify for parallelism, then by itself, only one process will be reading the hundred million records. However if it qualifies for parallelism and the server has 16 processors, then you will now have 16 threads each trying to get a piece of the hundred million records powered by their own processor. I’ve seen this bring many servers to their knees.

One indication that parallelism is occurring on your system is when you run sp_who2 and you see the same SPID listed more than once. This is indicative of a large query being broken into multiple streams only to join back into a single result later. Some streams may finish their operations prior to other streams completions. When this happens it results in a wait type of CX_Packet.  When this wait type is common, then you are waiting for parallel streams to finish.

As a summary, you do not want parallelism to be enabled unless you are sure that:

  1. You have more than enough resources
  2. You have a well optimized SQL Server
  3. Your server is mainly used for ETL purposes

SQL Current Date

To get the current date in SQL Server use the GETDATE() function. Here is an example:

SELECT CurrentDate = GETDATE()
SQL-Current-Date-GetDate

Current Date

GetDate() returns a datetime data type value.

There are also other methods in SQL Server that will return the current date. These are less commonly used. In fact, I have never personally had a need to use any of them except GETDATE().

SELECT SYSDATETIME()
SELECT SYSDATETIMEOFFSET()
SELECT SYSUTCDATETIME()
SELECT CURRENT_TIMESTAMP
SELECT GETDATE()
SELECT GETUTCDATE()
 

sql current date alternative

KEEPFIXED PLAN Query Hint

The KEEPFIXED PLAN query hint is used in order to retain the original execution plan used to initially compile a statement. It is typically used to disallow a recompile, especially in regards to a statement that utilizes temp table or a table that changes it’s percentage of records frequently resulting in triggering a recompile for a statement.

To use, specify OPTION (KEEPFIXED PLAN) immediately after the statement that you do not want to recompile.

SELECT *
FROM #Temp
OPTION (KEEPFIXED PLAN)

Temp tables frequently recompile due to the change in statistics. When using temp tables within stored procedures, this can be a disadvantage. To get around the recompile, either use table variables (indexed with constraints) or use the KEEPFIXED PLAN query hint.

Recompiles typically happen when the percentage of a tables (or temp tables) rows change by 500 and the cardinality (or uniqueness) changes by 20%.

MAXDOP Query Hint

The MAXDOP query hint is used to implicitly specify the maximum degree of parallelism to use for a specific statement.  The degree of parallelism is roughly defined as how many separate processors to utilize for a single query.  To set this, use the following convention:

SELECT *
FROM Sales.SalesOrderDetail
OPTION (MAXDOP 1)

This will force only one instance of the SPID to be spawned. To allow more instances of a SPID to be spawned replace the 1 with the maximum number.

This needs to be specified after each statement. It is not specific to the statement.

As with all query hints, it is recommended that you know internally what you are controlling by specifying this hint. While this specific hint is relatively harmless if you specify “1″ it is still recommended you understand it’s effects.

Configuring Parallelism

Parallelism is a powerful feature in SQL Server designed to allow greater bandwidth for high impact queries. Most of the time however, I’ve seen too many queries qualifying for parallelism and essentially bringing the SQL Server box to it’s knees.

To understand parallelism, we need to understand SPIDs (Server Process ID’s). These SPID’s act essentially as threads on a SQL Server. Normally there is one SPID assigned to each database connection. Sometimes, if the query is determined to be expensive, a SPID can be split out into multiple execution threads. This is known as parallelism.

Cost Threshold for Parallelism

Prior to breaking a SPID out into multiple threads, there is a cost determination. This is called the cost threshold for parallelism (set in seconds) and is set at the server-level. The idea is to allow parallelism when a query is estimated to run over a certain number of seconds.  Generally, the determining factor is the estimated number of rows that will be returned either in the main query or an inner query within the execution plan.  To set parallelism right click on the Server Instance in Management Studio. And select “properties”.

SSMS Context menu for Server Instance

SSMS Context menu for Server Instance

Once properties is selected, a new window pops up displaying a more detailed server settings. We want to select on “Advanced”

Cost Threshold for Parallelism Setting

Cost Threshold for Parallelism Setting

Now, in the lower middle of the screen we see the settings for Parallelism. The main two we are concerned with the is the cost threshold and the Max Degree of parallelism.

What Settings to Choose?

You need to choose settings that compliment both the primary function of the SQL Server instance, and the power of hardware you have available. Generally speaking, if the server instance is catered toward end-users and you have a lot of throughput (or connections) hitting the server, then you do not want queries to qualify for parallelism easily.

As an example, imagine you have a common stored procedure accessed by all users that is very sluggish and performs too many reads on every execution. If this query were to qualify for parallelism, then every time it is executed, it would spawn multiple threads (potentially equal to the number of processors). This behavior can bring your SQL Server to it’s knees. Yes, the execution may be quicker for each single user, however the additional resources used may spill over and cause an IO or CPU bottleneck.

With this in mind, for OLTP systems, it is generally better to set the cost for threshold higher, somewhere between 10 and 30. This way, less queries will use less resources. Now there is one additional option, which is, how many threads to spawn once we do qualify for parallelism. To set this you will use the Max Degree of Parallelism option.  Setting this option to 1 will completely disable all parallelism.  This is good to do when troubleshooting slowness issues on a server.  This option is settable using a query hint.

If your server’s primary function is as a data warehouse, then it is plausible that the parallelism option may be set low as there will be relatively few queries running concurrently and therefore can take more of the physical resources available to facilitate quicker executions.

MAXDOP Query Hint

This is probably the query hint I use the most, and the value I use the most is “1″. This is because when creating procedures that contain fairly extensive statements, I usually do not want them to consume resources if things get out of control.

Set the hint this way:

SELECT *
FROM Sales.SalesOrders
OPTION (MAXDOP 1)