EXERCISE TOPIC▼
SQL EXERCISES▼
SQL 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 | Archived exercise | Move person in procedure, and log if OK
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.
You can learn how to do this exercise on the relevant Wise Owl classroom training course (sadly for the moment only in the UK).
Software ==> | SQL (198 exercises) |
Version ==> | Any version of SQL Server |
Topic ==> | Archived (70 exercises) |
Level ==> | Average difficulty |
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.
If you haven't already done so, run the stored procedure in the above folder to generate a database of training courses and delegates.
This exercise will create a procedure to allow you to move a person to a different organisation. Your procedure will begin in a similar way to this:

The two arguments are the person to move, and the organisation to move them to
Create a log table to hold a record of every successful move:

Create 2 columns as shown here - iniitally your table will be empty
Write your stored procedure so that it:
- Begins a transaction
- Inserts a record in the log table recording that a person has been moved
- Runs an UPDATE statement to change the person's OrgId column value
- Commits the transaction if this doesn't generate an error, or rolls it back otherwise
Try testing your stored procedure - here are 3 suggestions to try:

Existing constraints will ensure that you can only move someone to a company which already exists
Optionally, save the code to generate your stored procedure as Log person moves.sql, then close it down.