The OUTPUT clause is supported in T-SQL for modification statements, which you can use to return information from modified rows. This clause, even if not as popular as others, is useful in multiple scenarios, for example:
- for auditing purposes
- confirmation messages
- archiving your data
It can be used to return information about every row affected by an INSERT, UPDATE, DELETE or MERGE operation.
You can find here some detailed information about the OUTPUT clause:
– https://msdn.microsoft.com/en-us/library/ms177564.aspx
– http://www.tech-recipes.com/rx/47032/a-beginners-guide-to-the-output-clause-in-sql-server/
If you feel that you have mastered this topic, test your knowledge with the following SQL quiz:
Quiz-summary
0 of 10 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Information
Are you ready to test your knowledge of working with the OUTPUT clause with the following quiz?
This week’s test has a difficulty level of: MODERATE
Quiz subject: Working with the OUTPUT clause
Good luck!
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 10 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- Answered
- Review
-
Question 1 of 10
1. Question
Which of the following keywords must be used to prefix the column names in the OUTPUT clause?
Choose all that apply:Correct
Incorrect
-
Question 2 of 10
2. Question
It is possible to redirect the results of the OUTPUT clause in a table, by adding an INTO clause.
Correct
Incorrect
-
Question 3 of 10
3. Question
What does this code do?
INSERT INTO RecipesHistory(IceCreamId, IngredientId, Quantity, ValidFrom, ValidTo)
OUTPUT
inserted.IceCreamId, inserted.IngredientId, inserted.Quantity, inserted.ValidFrom, inserted.ValidTo
INTO ExpiredRecipes_Temp(IceCreamId, IngredientId, Quantity, ValidFrom, ValidTo)
SELECT IceCreamId, IngredientId, Quantity, ValidFrom, ValidTo
FROM Recipes
WHERE ValidTo < GETDATE()
Correct
Incorrect
-
Question 4 of 10
4. Question
You can use the OUTPUT clause to return information about inserted and updated rows, but not about deleted rows.
Correct
Incorrect
-
Question 5 of 10
5. Question
What does the following code do?
UPDATE Recipes
SET Quantity = Quantity + 0.5
OUTPUT
inserted.Id,
deleted.Quantity AS Previous_Qty,
inserted.Quantity AS Current_Qty
WHERE IngredientId = 22 --(Milk)Correct
Incorrect
-
Question 6 of 10
6. Question
In INSERT, UPDATE, and DELETE statements, you can only refer to columns from the target
table in the OUTPUT clause. In a MERGE statement you can refer to columns from both the
target and the source.Correct
Incorrect
-
Question 7 of 10
7. Question
How many OUTPUT clauses can a single statement have?
Correct
Incorrect
-
Question 8 of 10
8. Question
How do you determine which action affected the OUTPUT row in a MERGE
statement?Correct
Incorrect
-
Question 9 of 10
9. Question
When referring in the OUTPUT clause to columns from the inserted rows, when should
you prefix the columns with the keyword inserted?Correct
Incorrect
-
Question 10 of 10
10. Question
Which of the following is only possible when using the MERGE statement in regard to
the OUTPUT clause?Correct
Incorrect