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

  • Option 4 Nikhildas Cochin , INDIA

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

    — No OUTPUT table with name Updated.

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

    — No OUTPUT table with name Updated.

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

    — Returns wrong value. Oldpay displays new value and NewPay displays old value.

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

    –Correct answer.

    Thanks

    Shree

    Bangalore India

    Reply
  • Ramakrishnan Srinivasan
    August 14, 2011 7:07 pm

    The right option is 4. Reason:

    1. There is no “UPDATED” magic table – Hence, options 1 & 2 are invalid
    2. When you update, the incremented value will reside in INSERTED, and the old value, in DELETED. In Query 3, this is inverted. Hence, it would give a logically incorrect output.

    Ramakrishnan RS
    Mysore, India

    Reply
  • Alok Chandra Shahi
    August 14, 2011 7:17 pm

    Correct Option Is Option 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

    Because we want OldPay and NewPay of Employee.And Output Clause Perform with “Deleted” in Update/Delete Query and “Inserted” in Update/Insert Query.And the right query is mention in Option 4 where we are using Deleted.Hourly to get Old value that is replaced, and Inserted.Hourly for new value that is replaced value.

    Alok Chandra Shahi
    India

    Reply
  • dilipkumarjena
    August 14, 2011 8:23 pm

    HI Pinal Sir,

    The Correct Option for the above question 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

    Explanation :

    Given : We have an HourlyPay table and are giving all hourly employees a $1 raise.

    The requirement is When we run the update statement we want to see the EmpID, OldPay, and NewPay.

    The New Pay will be in Inserted Output table because It is the new value and gets updated when we update or Inserted so to get the required Fields : 1) EmpId is common so any Output will fetch the required value, Old Pay will be deleted and Inserted with a new value so we have Deleted Output table hence Deleted.hourly is correct Option.

    The Only Option that has this combination is Option 4.

    Why Other Options are wrong:

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

    Answer) There is nothing called updated Output table in SQL SERVER so Option Ruled Out.

    Option 2) UPDATE HourlyPay SET Hourly = Hourly + 1

    OUTPUT Deleted.EmpID , Updated.Hourly as OldPay, Deleted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Answer) There is nothing called updated Output table in SQL SERVER so Option Ruled Out.

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

    Answer) The Abouve query gives Output that’s all right but the New Pay and Old Pay values are interchanged so definitely a very wrong Output.

    Hence the Correct Option is 4 th optiom.

    and a very happy Advance Independece Day to you and all my country people!!!!

    DILIP KUMAR JENA
    Country : INDIA

    Reply
  • Gopalakrishnan Arthanarisamy
    August 14, 2011 8:47 pm

    Correct Answer is # 4.

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

    Abobve query displays Employee ID and the Old Pay from the Deleted table and the New Pay from the Inserted table.

    Gopalakrishnan Arthanarisamy
    Unisys, Bangalore, India

    Reply
  • Has to be 4; no UPDATED table and option 3 would give the wrong prices

    Texas, USA

    Reply
  • #4.

    Deleted and Inserted are the correct temp tables to Output on to show the records before and after the update for comparison

    Reply
  • Kalyanasundaram.K
    August 14, 2011 11:57 pm

    Answer Option : 4

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

    Chennai, TamilNadu, India

    Reply
  • Hi Pinal,
    The correct option is 4.
    Explanation- A per the blog Update action+output will give Insert+Delete tables
    Now as the first 2 options does not contain Insert so they are opted out.
    The requirement is that when Update statement is run we can see the EmpID, OldPay, and NewPay. that is previous pay(Deleted.Hourly) and the new pay(Inserted.Hourly) of the employee which can only be achieved from the below update statement
    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL.
    This cannot be achieved using option 3 as pay of the employee
    before updation will not get displayed under OldPay and pay of the employee after updation will not get displayed under NewPay.

    Thanks,
    Manik Dey
    Country :- India

    Reply
  • correct answer no : 4
    shekhar gurav.
    country : India

    Reply
  • Option 4 will yeild the desired results:
    4.UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Country: United States

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

    Chetan
    Country: USA

    Reply
  • Hi Pinal,

    Challenge:
    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

    Correct Answer:
    The correct answer is #4:
    UPDATE HourlyPay SET Hourly = Hourly + 1
    OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay
    WHERE Hourly IS NOT NULL

    Explanation:
    Choice #1 won’t compile because of the phrase “Updated.Hourly as NewPay” in the OUTPUT clause. It should be “Inserted.Hourly as NewPay”. There is no “Updated” table to query against.

    Choice #2 also won’t compile due to the phrase “OUTPUT Deleted.EmpID , Updated.Hourly as OldPay, Deleted.Hourly as NewPay”. It should be “OUTPUT Deleted.EmpID , Deleted.Hourly as OldPay, Inserted.Hourly as NewPay”. Here also, there is no “Updated” table to query against.

    Choice #3 will compile, but will show the results as EmpID, new hourly rate as OldPay and old hourly rate as NewPay. The pay rate labels are inversed and incorrect.

    Choice #4 is the correct SQL to show the EmpID, the old hourly rate as OldPay and the new hourly rate as NewPay.

    Country:
    United States

    Thanks for the knowledge!

    Regards,

    Bill Pepping

    Reply
  • Answer is #4

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

    Since you are looking for the old pay you want the Deleted.Hourly table for that one and the Inserted.Hourly for the new pay. The other queries have this incorrect or are incorrectly looking fora field in an Updated table.

    Reply
  • correct option is 4

    I’m from INDIA

    Reply
  • This blog answer is : 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
  • 4)

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

    Reply
  • 4)

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

    Location : India

    Reply
  • Option 1 and Option 3 are correct.
    Ramdas,NC,USA

    Reply

Leave a Reply