solving sql server problems for millions of dbas and developers since 2006


Identify and resolve SQL Server problems BEFORE they happen with SQL diagnostic manager

SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers


SQL Product Highlight

Idera - SQL diagnostic manager

SQL diagnostic manager is a powerful performance monitoring and diagnostics solution that proactively alerts administrators to health, performance or availability problems within their SQL Server environment via a central console or mobile device. SQL diagnostic manager minimizes costly server downtime by providing agent-less, real-time monitoring and customizable alerting for fast diagnosis and remediation of SQL Server performance and availability problems. SQL diagnostic manager also provides a 'community' environment where, using Idera's IntelliFeed(TM) technology DBAs form a community t

Learn more!








SQL Server 2008 consume output directly from the OUTPUT command

By: | Read Comments | Print

Chad is an Architect, Administrator and Developer with technologies such as SQL Server, .NET, and Windows Server.

Related Tips: More

Most of you are aware that Sql Server 2005 introduced the OUTPUT clause, which provided functionality to stream records affected by a write-based statement (i.e. insert/update/delete) into a table variable, which you could then use for other purposes (perhaps to log, or to archive data from a non-partitioned table in a single statement vs. a read/insert/delete operation). This was a great addition to the server, however one thing you didn't have the ability to do was directly consume the OUTPUT row-set with an outer DML statement (for example, consuming the result set produced by the OUTPUT with a select statement wrapped around the write-based statement). This however is changing with Sql 2008, as you will now be able to consume the OUTPUT result-set directly with a wrapped statement.

So, for example, this simple OUTPUT example works on Sql 2005 today, whereby the affected records of the insert statement are OUTPUT to the display/client:

use tempdb;

-- Table to output records into...
if object_id('tempdb..#testingOutput') > 0
    drop table #testingOutput;
create table #testingOutput (id int, modified_date datetime default getdate());
-- Table to test DML on...
if object_id('tempdb..#testingDml') > 0
    drop table #testingDml;
create table #testingDml (id int);

-- Perform a simple insert with output to the screen/user...
insert    #testingDml (id)
output    inserted.id
select    top 10 a.object_id
from    sys.objects a
order by a.object_id;

Additionally, the following example works as well, capturing the OUTPUT result-set into a table variable that can be re-used for a select statement following the initial DELETE statement:

-- OUTPUT capture...
-- Delete all the data, capturing the affected records into the table variable...
delete    #testingDml
output    deleted.id
into    #testingOutput (id);
-- Read the data back from the table variable...
select    id, modified_date
from    #testingOutput;

However, the following statement is not allowed in Sql 2005, where an attempt is made to directly consume the OUTPUT record-set by an outer INSERT...SELECT statement:

-- This won't work in Sql 2005 (direct consumption of the OUTPUT result-set)
insert    #testingOutput (id, modified_date)
select    id, getdate()
from    (    -- NOTICE the embedded INSERT statement here...
            insert    #testingDml (id)
            output    inserted.id
            select    a.object_id
            from    sys.objects a
            except    -- Don't get id's that already exist in the output data...
            select    o.id
            from    #testingOutput o
        ) output_data;

If you take this same script and run it against the latest Sql 2008 CTP, you'll notice it works perfectly. There are some restrictions on the usage however, some of which include:

  • The nested write-based statement is only allowed when the consuming outer-select statement is the immediate source for an outer INSERT statement
  • The outer SELECT statement can't include a JOIN, EXCEPT, UNION, INTERSECT, GROUP BY, etc. type statement in correlation with the nested write statement
  • The outer SELECT statement CAN include a simple where clause, but nothing like a 'where not exists (subquery here)', or any other where clause that contains a sub-query (like an IN clause for example with a nested select)

You may be wondering where this would be useful, and a BIG place that it will become useful is when used in conjunction with another new feature in Sql 2008 - the MERGE statement. The MERGE in Sql 2008 is another write-based statement (in addition to the existing insert, update, delete statements), and also includes other interesting enhancements that can be used in conjunction with this new functionality for scenarios like a data-warehouse with a slowly changing dimension. Yes, I'll be posting another new post shortly that will include a breakdown of the new MERGE functionality, including some advanced usages for just these types of scenarios.

Enjoy!

Chad Boyd ~~~ This posting is provided "AS IS" with no warranties, and confers no rights. Use of any included script samples are subject to the terms specified at http://www.mssqltips.com/disclaimer.asp and http://www.mssqltips.com/copyright.asp.



Related Tips: More | Become a paid author


Last Update: 10/31/2007

Share: Share 






Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Try the award winning SQL diagnostic manager as a free 14-day trial!

Web-based SQL Server monitoring whenever, wherever.

You don't know, what you don't know about SQL Server... Customized Consulting and Training

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Valuable SQL Server web casts on Performance Tuning, Development, Administration, SSIS and more...


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com