SQL Server output经典使用

时间:2021-01-01 07:01:32
分类: sql2012-02-16 18:17 409人阅读 评论(0) 收藏 举报
OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可以看作是"返回结果的DML".INSERT,DELETE,UPDATE均支持
OUTPUT子句.在OUTPUT子句中,可以引用特殊表inserted和deleted.使用inserted和deleted表与在触发器中使用的非常相似.

在INSERT,DELETE,UPDATE中OUTPUT的区别 
1.对于INSERT,可以引用inserted表以查询新行的属性.

2.对于DELETE,可以引用deleted表以查询旧行的属性.

3.对于UPDATE,使用deleted表查询被更新行在更改前的属性,用inserted表标识被更新行在更改后的值.

输出方式: 
1.可以输出给调用方(客户端应用程序)

2.输出给表

3.两者皆可.

应用:  
一.带有OUTPUT的INSERT的应用 
对于包含自增列的表执行多行insert语句,同时想知道新的标识值时,在INSERT中使用OUTPUT子句非常方便.对于单行INSERT语句,
这不成问题:SCOPE_IDENTITY函数即可实现.

-- Generating Surrogate Keys for Customers   
USE tempdb;   
GO   
IF OBJECT_ID('dbo.CustomersDim') IS NOT NULL   
  DROP TABLE dbo.CustomersDim;   
GO   
   
CREATE TABLE dbo.CustomersDim   
(   
  KeyCol      INT          NOT NULL IDENTITY PRIMARY KEY,   
  CustomerID  NCHAR(5)     NOT NULL,   
  CompanyName NVARCHAR(40) NOT NULL,   
  /* ... other columns ... */   
);   
   
-- Insert New Customers and Get their Surrogate Keys   
DECLARE @NewCusts TABLE   
(   
  CustomerID NCHAR(5) NOT NULL PRIMARY KEY,   
  KeyCol     INT      NOT NULL UNIQUE   
);   
   
INSERT INTO dbo.CustomersDim(CustomerID, CompanyName)   
    OUTPUT inserted.CustomerID, inserted.KeyCol   
    INTO @NewCusts   
    -- OUTPUT inserted.CustomerID, inserted.KeyCol   
  SELECT CustomerID, CompanyName   
  FROM Northwind.dbo.Customers   
  WHERE Country = N'UK';   
   
SELECT CustomerID, KeyCol FROM @NewCusts;   
GO

注意代码中被注释掉的第二个OUTPUT子句,后面没有INTO子句.如果还要输出返回给调用方,取消注释即可.这样,
INSERT语句将包含两个OUTPUT子句.

示例2.

USE AdventureWorks;   
GO

CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))

DECLARE @TmpTable TABLE (ID INT, TEXTVal VARCHAR(100))   
  
INSERT TestTable (ID, TEXTVal)   
OUTPUT Inserted.ID, Inserted.TEXTVal INTO @TmpTable   
VALUES (1,'FirstVal')   
INSERT TestTable (ID, TEXTVal)   
OUTPUT Inserted.ID, Inserted.TEXTVal INTO @TmpTable   
VALUES (2,'SecondVal')   
  
SELECT * FROM @TmpTable   
SELECT * FROM TestTable   
  
DROP TABLE TestTable   
GO   
USE AdventureWorks;   
GO

CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))   
  
INSERT TestTable (ID, TEXTVal)   
OUTPUT Inserted.ID, Inserted.TEXTVal   
VALUES (1,'FirstVal')   
INSERT TestTable (ID, TEXTVal)   
OUTPUT Inserted.ID, Inserted.TEXTVal   
VALUES (2,'SecondVal')

DROP TABLE TestTable   
GO

二.带有OUTPUT的DELETE的应用. 
如果要删除数据的同时,还需要记录日志,或者归档数据.在DELETE中使用OUTPUT子句在适合不过了.

USE AdventureWorks;   
GO   
CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))

DECLARE @TmpTable TABLE (ID INT, TEXTVal VARCHAR(100))

INSERT TestTable (ID, TEXTVal)   
VALUES (1,'FirstVal')   
INSERT TestTable (ID, TEXTVal)   
VALUES (2,'SecondVal')   
  
DELETE   
FROM TestTable   
OUTPUT Deleted.ID, Deleted.TEXTVal INTO @TmpTable   
WHERE ID IN (1,2)   
  
SELECT * FROM @TmpTable   
SELECT * FROM TestTable

DROP TABLE TestTable   
GO

三.带有OUTPUT的UPDATE的应用  
USE AdventureWorks;   
GO   
CREATE TABLE TestTable (ID INT, TEXTVal VARCHAR(100))

DECLARE @TmpTable TABLE (ID_New INT, TEXTVal_New VARCHAR(100),ID_Old INT, TEXTVal_Old VARCHAR(100))

INSERT TestTable (ID, TEXTVal)   
VALUES (1,'FirstVal')   
INSERT TestTable (ID, TEXTVal)   
VALUES (2,'SecondVal')

UPDATE TestTable   
SET TEXTVal = 'NewValue'   
OUTPUT Inserted.ID, Inserted.TEXTVal, Deleted.ID, Deleted.TEXTVal INTO @TmpTable   
WHERE ID IN (1,2)   
  
SELECT * FROM @TmpTable   
SELECT * FROM TestTable

DROP TABLE TestTable   
GO   

SP:
ALTER           procedure [dbo].[sp_I_InterfaceJobEvent]
@JobServer varchar(30) =NULL,
@JobType varchar(30)= NULL,
@JobStartTime datetime= NULL,
@JobEndTime datetime= NULL,
@SW1_FileType varchar(10)= NULL,
@SW2_SystemDate varchar(10)= NULL,
@SW3_GenDate varchar(10)= NULL,
@SW4_Email varchar(2)= NULL,
@JobStatus varchar(20) =NULL,
@JobMessage varchar(1000)= NULL,
@JobReturnCode char(2)= NULL,
@FileSearchPattern varchar(50)= NULL,
@FileListMatched varchar(5000)= NULL,
@SourceApplication varchar(3)= NULL,
@OptionalQualifier varchar(30)= NULL,
@SystemDate varchar(10)= NULL,
@LockBy varchar(30)= NULL,
@LockDate datetime= NULL,
@LockWorkstation varchar(30)=null
as
begin Insert into InterfaceJobEvent
(
[JobServer],
[JobType],
[JobStartTime],
[JobEndTime],
[SW1_FileType],
[SW2_SystemDate],
[SW3_GenDate],
[SW4_Email],
[JobStatus],
[JobMessage],
[JobReturnCode],
[FileSearchPattern],
[FileListMatched],
[SourceApplication],
[OptionalQualifier],
[SystemDate],
[LockBy],
[LockDate],
[LockWorkstation]
)
output inserted.JobID
values (
isnull(@JobServer,''),
isnull(@JobType,''),
isnull(@JobStartTime,GETDATE()),
@JobEndTime,
isnull(@SW1_FileType,''),
@SW2_SystemDate,
@SW3_GenDate,
isnull(@SW4_Email,''),
isnull(@JobStatus,''),
isnull(@JobMessage,''),
isnull(@JobReturnCode,''),
isnull(@FileSearchPattern,''),
isnull(@FileListMatched,''),
isnull(@SourceApplication,''),
isnull(@OptionalQualifier,''),
isnull(@SystemDate,''),
isnull(@LockBy,''),
@LockDate,
isnull(@LockWorkstation,'')
) --if @@rowcount > 0
--begin
-- return @@rowcount
--end
--else
-- return 0
--end end
 Dim int_Jobid As Int64
Try
With adtInterfaceJobEvent.InsertCommand .Parameters("@SW1_FileType").Value = SW1_FileType
.Parameters("@SW2_SystemDate").Value = SW2_SystemDate
.Parameters("@SW3_GenDate").Value = SW3_GenDate
.Parameters("@SW4_Email").Value = SW4_Email
.Parameters("@FileSearchPattern").Value = FileSearchPattern
.Parameters("@FileListMatched").Value = FileListMatched
.Parameters("@SourceApplication").Value = SourceApplication
.Parameters("@OptionalQualifier").Value = OptionalQualifier
.Parameters("@SystemDate").Value = SystemDate
.Parameters("@JobServer").Value = Environment.MachineName
.Parameters("@JobStartTime").Value = DateTime.Now
.Parameters("@JobType").Value = JobType
.Parameters("@JobStatus").Value = JobStatus End With If SqlConnection_DISDB.State = ConnectionState.Closed Then
SqlConnection_DISDB.Open()
End If int_Jobid = adtInterfaceJobEvent.InsertCommand.ExecuteScalar()