SQL | Temporary tables and table variables exercise | Build up a temporary table of doctors, companions and enemies

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> SQL  (198 exercises)
Version ==> Any version of SQL Server
Topic ==> Temporary tables and table variables  (9 exercises)
Level ==> Relatively easy
Subject ==> SQL training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

First (if you haven't already done so) run the script shown above to generate the Doctor Who training database.

The aim of this exercise (which is fully explained in the following pages) is to create a temporary table called #Characters containing all of the doctors, companions and enemies in the database.  The idea is that you should be able to display these as follows:

-- show results

SELECT * FROM #Characters

ORDER BY CharacterName DESC

This should show the following rows: 

First few characters

The first few of the 109 "characters" in the database (giving the id number and type of each).


Start a query, and write code to delete any temporary table called #Characters:

-- get rid of any old copy of temporary table

IF object_id('tempdb.dbo.#Characters', 'U') is not null

DROP TABLE #Characters

Write an instruction to put the doctors into a new table.  The syntax is:

-- create a new table containing rows from an existing table


Column1 AS NewColumnAlias1,


ColumnN AS NewColumnAliasN,





You may need to cast the character type to a 10-character string, otherwise the query will create a 6-character string to accommodate the word Doctor and crash when you try to put the word Companion in it subsequently.

Here's what you should have so far when you display the rows in your temporary table in reverse name order:

The doctors

So far we have 12 doctors - time now to add some companions and enemies.


SQL Server will have created the CharacterId column as an identity column.  Tell it to let you add more rows by running this line:

-- allow more identity values to be inserted


Now use two separate statements to add the companions and enemies (from the tblCompanion and tblEnemy tables respectively) into your temporary table, using this syntax:

-- append rows into existing table

INSERT INTO #Characters (











When you've finished, every time that you run your entire query it should drop any old copy of the temporary table and create a new one containing 109 rows (as shown at the start of this exercise).

Optionally, save your query as Strange bedfellows.sql, then close it down.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post