Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | SQL (203 exercises) |
Topic ==> | Temporary tables and table variables (13 exercises) |
Level ==> | Relatively easy |
Subject ==> | SQL training |
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.
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
SELECT
Column1 AS NewColumnAlias1,
...,
ColumnN AS NewColumnAliasN,
INTO
#Characters
FROM
TableName
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 (
Column1,
...,
ColumnN
)
SELECT
ColumnOrValue1,
...,
ColumnOrValueN
FROM
TableName
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 find other training resources for the subject of this exercise here:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.