Solved

the output into clause and table variable

Posted on 2011-03-18
3
391 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
3 Comments
 
LVL 142

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now