SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Output Clause in Simple Examples – Day 14 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 2.
Every day one winner from United States will get Joes 2 Pros Volume 2.

Output

We will first begin our work with the OUTPUT clause, by diving into hands-on examples of deleting, inserting, and updating table data. Later, we will demonstrate logging these types of changes in a separate storage table. Note: The OUTPUT statement uses temporary INSERTED and/or DELETED tables. These memory-resident tables are used to determine the changes being caused by the INSERT, DELETE or UPDATE statements.

Delete Actions with Output

If you run the Reset script from chapter 14 of Book2 (SQLQueriesChapter14.0Setup.sql) then you see JProCo has five locations, two of them in Washington State. The DELETE query below will remove both the Seattle headquarters and the Spokane office.

SQL Server confirms that 2 rows were affected, but which 2 rows? It doesn’t say, and oftentimes you’re not interested in that detail. The OUTPUT clause displays the exact affected rows, even though they’re no longer present in your table.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_1

Let’s check to confirm that all WA locations have been removed. All WA locations have been removed from the Location table.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_2

Now reset the Location table to show all the original records. To do this rerun the current setup script (SQLQueriesChapter14.0Setup.sql)

Our DELETE query will again remove those same two records from the Location table. But first we will place an OUTPUT statement between the FROM and WHERE clauses. OUTPUT Deleted.* states that we want to see all deleted records and all of their fields in order. When we run this code, instead of saying “2 row(s) affected,” it will show us the actual deleted. The Deleted.* worked because the OUTPUT clause accessed the deleted memory-resident table.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_3

Insert Actions with Output

The OUTPUT keyword exposed a special type of temporary table, or tables, based on the type of action you take. In our example, it created a table called Deleted to show us the records it removed. However, if we INSERT records, OUTPUT will not be able to create a Deleted table (see Figure 14.4). If you reattempt this query with an OUTPUT table called Inserted. This should display what is now the sixth record in the Location table. (see both figures below).

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_4

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_5

Update Actions with Output

We now know that the OUTPUT keyword will create an Inserted or a Deleted memory-resident table based on the query action you are running. The OUTPUT clause creates an Inserted table when you run an INSERT statement, and it creates the Deleted table when you run a DELETE operation. When you run an UPDATE statement, it creates both tables.

Let’s think about the UPDATE statement for a moment. DELETE and INSERT each perform a single action – namely, they either add or remove records.

However, an UPDATE statement is a little different. While it also performs a single action (replaces an existing value with a new value), reflecting that change with an OUTPUT statement is a little more complex. If your manager asked you to track the changes made with an UPDATE statement, you really would want to capture two things: 1) the existing record and 2) the updated record.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_6

The OUTPUT clause handles this situation for you. When you run an UPDATE statement, the OUTPUT clause can create and populate both an Inserted and a Deleted table. This is a great way to see each old record next to the new record. Later we will see examples where the old and new records are entered side by side into the same storage table. Let’s see the UPDATE statement in action. First we’ll look just at Location 1, which is JProCo’s Seattle location as seen in the figure below.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_7

Now let’s run an UPDATE to change the city to Kirkland.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_8

Kirkland is now the city for Location 1. So if we were to use OUTPUT to generate the confirmation, would we want to see Seattle, or would we want to see Kirkland? As stated earlier, we can look at either one or both. We can see the record we just got rid of (i.e., the Seattle record), the record we just gained (i.e., the Kirkland record), or we can see both together.

Let’s change the city once more to Tacoma and then observe OUTPUT generating both Deleted and Inserted tables. The Figure below shows us both the old record and the new record.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Output Clause in Simple Examples - Day 14 of 35 j2p_14_9

Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLQueriesChapter14.0Setup.sql script from Volume 2.

Question 14

You have an HourlyPay table and are giving all hourly employees a $1 raise. When you run the update statement you want to see the EmpID, OldPay, and NewPay. What code will achieve this result?

  1. UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Updated.Hourly as NewPay
    WHERE Hourly IS NOT NULL
  2. UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Updated.Hourly as OldPay, Deleted.Hourly as NewPay
    WHERE Hourly IS NOT NULL
  3. UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Inserted.Hourly as OldPay, Deleted.Hourly as NewPay
    WHERE Hourly IS NOT NULL
  4. UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 2.
Winner from India will get Joes 2 Pros Volume 2.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Ranking Functions – Advanced NTILE in Detail – Day 13 of 35
Next Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Data Row Space Usage and NULL Storage – Day 15 of 35

Related Posts

59 Comments. Leave new

  • Rene Alberto Castro Velasquez
    August 14, 2011 7:21 am

    Correct answer is N0. 4, because the OUTPUT statement uses temporary INSERTED and/or DELETED tables. Thus, answers 1 and 2 are incorrect because they use a non-existent table called UPDATED, and answer 3 presents the Hourly fields with the incorrect order: OldPay must be in the DELETED table and NewPay must be in the INSERTED table. Answer 4 shows the Hourly fields with the correct order.
    Rene Castro
    El Salvador

    Reply
  • 4) UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    1) and 2) are wrong because there is no column prefix called Updated; only INSERTED and DELETED
    3) will show old hourly value as new and the old one as new

    Leo Pius
    USA

    Reply
  • Shatrughna Kumar
    August 14, 2011 7:28 am

    Correct option is 4.
    New Delhi

    Reply
  • Jason P. Brown (@bensdad03)
    August 14, 2011 7:58 am

    Number 4 would be correct. The Employee ID and the Old Pay would be from the Deleted table and the New Pay would have to come from the Inserted table. Number 3 would be close except that the pays are switched. Since there is no updated table, 1 and 2 would never work.

    Jason
    Georgia – USA

    Reply
  • Option 4 is correct

    4.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Reply
  • Correct answer is #4. In first two answers there is a reference to non-existing UPDATED table (there is no such table) and in the #3 the inserted and deleted tables used in wrong orders.

    So, only #4 correctly represents the desired output

    I am from USA

    Reply
  • Partha Pratim Dinda
    August 14, 2011 11:40 am

    ans is 4:
    4.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL
    Because update use two temporary table inserted and Deleted . In deleted it store old data and in inserted it store the new data . So only option 4 is correct.

    https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-2017

    Partha
    India,

    Reply
  • Varinder Sandhu
    August 14, 2011 11:41 am

    Correct answer is 4 as

    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    1 and 2 and wrong because there used wrong table updated

    3 is wrong because of

    Deleted.Hourly as NewPay
    Inserted.Hourly as OldPay

    Varinder sandhu (India)

    Reply
  • Hi,

    Correct Ans is 4
    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Output Deleted.EmpID, Deleted.Hourly as OldPay Display Employee ID and Hourly Pay of Before Update

    Output Inserted.Hourly Hourly Pay after Update.

    I am from India.

    Reply
  • Rajneesh Verma
    August 14, 2011 2:19 pm

    Hi, Answer 1,2 are incorrect because it has Updated while should have Deleted/Inserted.
    3 incorrect because it has ” OUTPUT Deleted.EmpID , Inserted.Hourly as OldPay, Deleted.Hourly as NewPay
    WHERE Hourly IS NOT NULL” Mixed of deleted/inserted
    Only answer 4 is correct ” UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL”
    It fulfill all conditions.

    Thanks….
    Rajneesh Verma
    (INDIA)

    Reply
  • Option 4 would be the right query for getting the appropriate output i.e EmpID, OldPay, and NewPay.

    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    As we are Updating the HourlyPay for all Employee’s [EmpID], By executing the above query
    the output will be the Employee ID & Old Pay [Deleted one] & New Pay [Inserted One]

    Option 3 would result in the Old Pay and New Pay value interchanged. Which we do not want.
    Option 2 & 1 would not work

    -Mukesh (India)

    Reply
  • Anish Shenoy.P
    August 14, 2011 2:57 pm

    Hi Sir,

    The correct option is 4th Query.

    Option 1 and 2 are incorrect query because it is referencing an “Updated” memory-resident table which is invalid.

    Option 3.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Inserted.Hourly as OldPay, Deleted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    The query is proper but the referencing for OldPay and NewPay are interchanged.

    Option 4.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Is the correct answer as Deleted.Hourly is the OldPay and Inserted.Hourly is the NewPay which was updated.

    P.Anish Shenoy
    INDIA,Bangalore,Karnataka

    Reply
  • Answer is Option 4;

    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Because old pay come from deleted table record and new record from inserted table record.

    Thank you
    Country:India

    Reply
  • PC Rao (@pcrao_kode)
    August 14, 2011 3:53 pm

    Answer 4 i.e.
    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay WHERE Hourly IS NOT NULL

    I’m from UK

    Reply
  • Nakul Vachhrajani
    August 14, 2011 4:18 pm

    Correct Answer: #4
    Reason: The DELETED table would give us information about the OldPay, while the INSERTED table would give us information about the NewPay. The EmpID is the key, and in an update operation such as this, it does not make a difference whether we fetch it from the DELETED or from the INSERTED tables.

    Country of residence: India

    Reply
  • Aditya Bisoi (@AdityaBisoi07)
    August 14, 2011 4:49 pm

    Question 14

    Ans : 4 –
    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Chennai , INDIA

    Reply
  • Option 4 is correct because option 1 and 2 are referring to UPDATED which doesn’t exist. Option 3 is wrong because Inserted will have NewPay and its used as Old Pay.

    So Option 4 is correct.

    Regards,

    Sudhir Chawla, New Delhi (INDIA)

    Reply
  • Correct Option is 4.

    4.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL
    This will produce the desire result.
    (Sale, Nigeria)

    Reply
  • Dayanand Singh
    August 14, 2011 6:38 pm

    Correct Answer is Option 4

    Explanation: As you had explained that whenever any row or value is deleted form table, it is stored in ‘deleted’ table and on inserting any row or value the value is also stored in ‘inserted’ table. In case of Update, old value is first deleted and then new value is inserted. So, in this case old pay can be known from deleted table and new pay can be known from inserted table.Hence, Option 4 is correct as per the question.

    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Country – INDIA (Gujarat)

    Reply
  • Basavaraj Biradar
    August 14, 2011 6:39 pm

    Correct Answer is:4

    Option 1 & 2 are not correct as we don’t have any logical table named updated.
    Option 3 is not correct, because in case of oldpay we are using inserted logical table which gives new pay and incase of newpay we are using deleted logical table which will give the old value i.e. Value prior to update. So the correct answer is option 4.

    Thanks,
    Basavaraj Biradar
    India

    Reply

Leave a Reply