Solved

the output into clause and table variable

Posted on 2011-03-18
3
395 Views
Last Modified: 2012-05-11
hi experts, i have this query
create table Reservation(
ID int identity (1,1) not null,
CustomerID int not null,
ArrivalDate datetime not null,
DepartDate datetime not null,
TotalAmount money not null,
DepositAmount as TotalAmount * .15);

declare @tblvar table
(
      ID int not null,
      CustomerID int not null,
      TotalAmount money not null
);
insert
into Reservation
      output inserted.ID, inserted.CustomerID, inserted.TotalAmount
into  @tblvar
      output inserted.*,
             GETDATE()
values(35, '11-10-08', '11-15-08', 400.00);

but i do not understand
1. the output into clause
2. the output clause
3.why it shows a single record?
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 150 total points
ID: 35164364
>but i do not understand
>1. the output into clause
that OUTPUT INTO clause saves the data into a table (variable), so you can proceed working on the data after the insert.
in this case, it's not really needed, visibly

>2. the output clause
the OUTPUT clause, without INTO, will just do what a SELECT would do with the inserted records.

>3.why it shows a single record?
because there is only 1 record inserted (VALUES calsue ...)


0
 
LVL 6

Assisted Solution

by:dan_mason
dan_mason earned 50 total points
ID: 35164706
The OUTPUT clause is explained in some more detail here:

http://technet.microsoft.com/en-us/library/ms177564.aspx
0
 
LVL 3

Assisted Solution

by:kumarnimavat
kumarnimavat earned 50 total points
ID: 35165306
SQL Server 2005 has new OUTPUT clause, which is quite useful. OUTPUT clause has accesses to inserted and deleted tables (virtual tables) just like triggers. OUTPUT clause can be used to return values to client clause. OUTPUT clause can be used with INSERT, UPDATE, or DELETE to identify the actual rows affected by these statements.

OUTPUT clause can generate table variable, a permanent table, or temporary table. Even though, @@Identity will still work in SQL Server 2005, however I find OUTPUT clause very easy and powerful to use. Let us understand OUTPUT clause using example.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question