Note that there are some explanatory texts on larger screens.

plurals
  1. POOutput columns not in destination table?
    text
    copied!<p><strong>SUMMARY:</strong><br> I need to use an <a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx" rel="nofollow noreferrer">OUTPUT clause</a> on an <a href="http://msdn.microsoft.com/en-us/library/ms174335.aspx" rel="nofollow noreferrer">INSERT</a> statement to return columns which don't exist on the table into which I'm inserting. If I can avoid it, <em>I don't want to add columns to the table to which I'm inserting</em>.</p> <p><strong>DETAILS:</strong><br> My FinishedDocument table has only one column. This is the table into which I'm inserting.</p> <p>FinishedDocument<br> -- DocumentID</p> <p>My Document table has two columns. This is the table from which I need to return data.</p> <p>Document<br> -- DocumentID<br> -- Description</p> <p>The following inserts one row into FinishedDocument. Its OUTPUT clause returns the DocumentID which was inserted. This works, but it doesn't give me the Description of the inserted document.</p> <pre><code>INSERT INTO FinishedDocument OUTPUT INSERTED.DocumentID SELECT DocumentID FROM Document WHERE DocumentID = @DocumentID </code></pre> <p>I need to return <em>from the Document table</em> both the DocumentID and the Description of the matching document from the INSERT.</p> <p>What syntax do I need to pull this off? I'm thinking it's possible only with the one INSERT statement, by tweaking the OUTPUT clause (in a way I clearly don't understand)?</p> <p>Is there a smarter way that doesn't resemble the path I'm going down here?</p> <p><strong>EDIT:</strong> SQL Server 2005</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload