WISE OWL EXERCISES
- Simple Queries (4)
- Setting criteria using WHERE (5)
- Calculations (7)
- Calculations using dates (4)
- Basic joins (8)
- More exotic joins (2)
- Aggregation and grouping (8)
- Views (5)
- Subqueries (5)
- Stored procedures (5)
- Variables (8)
- Parameters and return values (11)
- Testing conditions (1)
- Looping (3)
- Scalar functions (6)
- Transactions (5)
- Creating tables (5)
- Temporary tables and table variables (9)
- Table-valued functions (6)
- Derived tables and CTEs (13)
- Dynamic SQL (4)
- Pivots (2)
- Triggers (2)
- Archived (70)
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.
The answer to the exercise will be included and explained if you attend the course listed below!
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:
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:
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
SET IDENTITY_INSERT #Characters ON
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.