ALL SOFTWARE EXERCISES
- EXERCISES HOME PAGE (813)
- Access 2010 (66)
- Access VBA Macros (17)
- Advanced VBA (29)
- Excel 2010 (83)
- Excel 2013 (10)
- Excel 2016 (0)
- Excel VBA Macros (37)
- Power BI - Excel 2013 (25)
- Power BI - Excel 2016 (23)
- Power BI Desktop (26)
- PowerPivot 2010 (26)
- Report Builder 3.0 (42)
- SQL (156)
- SSAS - multidimensional (21)
- SSAS - tabular (29)
- SSIS Integration Services (40)
- SSRS Reporting Services (53)
- Visual Basic (45)
- Visual C Sharp (65)
- WPF - Visual C# (20)
- 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 (3)
- Stored procedures (3)
- Variables (4)
- Parameters and return values (4)
- Looping (1)
- Scalar functions (3)
- Transactions (2)
- Creating tables (3)
- Temporary tables and table variables (5)
- Table-valued functions (3)
- Derived tables and CTEs (8)
- Dynamic SQL (2)
- Pivots (2)
- Archived (70)
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.