DML_The OUTPUT Clause

Posted cdpj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DML_The OUTPUT Clause相关的知识,希望对你有一定的参考价值。

DML_The OUTPUT Clause

/**/
----------------------------------------------------------------------------------------------
--带有OUTPUT的INSERT语句
IF OBJECT_ID(dbo.T5,U) IS NOT NULL DROP TABLE dbo.T5
CREATE TABLE dbo.T5
(
keycol INT NOT NULL IDENTITY(1,1) CONSTRAINT PK_T5 PRIMARY KEY,
datacol NVARCHAR(40) NOT NULL 
);

--查询一下,可从这个 pubs.employee  表中插入值
SELECT * 
FROM pubs.dbo.employee
WHERE job_id = N10;

--SET IDENTITY_INSERT dbo.T5 ON;
--dbcc checkident(T1,RESEED,0)   --重置标识列为0
--dbcc checkident(T1,RESEED,1)   --重置标识列为0

INSERT INTO dbo.T5(datacol)
    OUTPUT inserted.keycol, inserted.datacol
    SELECT lname
    FROM pubs.dbo.employee
    WHERE job_id = N10;

SELECT * FROM T5

dbcc checkident(T5,RESEED,6)   --重置标识列为6,下一个标识值将从 7 开始
--a. 知识点
--在对标识列进行重新规划相关列值的时候,需采用 checkident函数对表中标识值进行规整
--1 dbcc checkident(‘表名’) 默认将表中的标识列的起始值初始化为 当前此列的最大值
--   dbcc checkident(‘表名’,NORESEED) 返回当前标识列最大值
--2 dbcc checkident(‘表名’,RESEED) 功能类似 dbcc checkident(‘表名’)
--3 dbcc checkident(‘表名’,RESEED,新标识列起始值) 手动指定新标识列起始值

--b.知识点,再回顾 IDENTITY属性(Chapter 8. Data Modification: The IDENTITY Property)

--将结果集导入到另外一个表中
DECLARE @NewRows TABLE(keycol INT, datacol NVARCHAR(40));
INSERT INTO dbo.T5(datacol)
     OUTPUT inserted.keycol, inserted.datacol
     INTO @NewRows
          SELECT lname
          FROM pubs.dbo.employee
          WHERE job_id = N10;

SELECT * FROM @NewRows;
----------------------------------------------------------------------------------------------
--带有OUTPUT的DELETE语句

SELECT * FROM dbo.Orders       --尽管 testdb 库下有 Orders,我们还是从 Northwind.dbo.Orders 插入数据
SELECT * FROM Northwind.dbo.Orders  
WHERE 

IF OBJECT_ID(dbo.OrdersB,U) IS NOT NULL DROP TABLE dbo.OrdersB;
SELECT * INTO dbo.OrdersB FROM Northwind.dbo.Orders  ;

SELECT * FROM testdb.dbo.OrdersB
-------------------------
DELETE FROM dbo.OrdersB
    OUTPUT
         deleted.OrderID,
         deleted.OrderDate,
         deleted.EmployeeID,
         deleted.CustomerID
    WHERE OrderDate < 19980101

SELECT * FROM dbo.OrdersB
-------------------------

--归档
sp_help Northwind.dbo.Orders   ( × )
sp_help dbo.Orders                      ( × )
sp_help Orders

-------------------------
DECLARE @OrdersBB TABLE(OrderID INT, OrderDate datetime, EmployeeID INT, cUSTOMERid NVARCHAR(10))
DELETE FROM dbo.OrdersB
    OUTPUT
         deleted.OrderID,
         deleted.OrderDate,
         deleted.EmployeeID,
         deleted.CustomerID
    INTO @OrdersBB
    WHERE OrderDate < 19980501

SELECT * FROM @OrdersBB
-------------------------        

----------------------------------------------------------------------------------------------
--带有OUTPUT的UPDATE语句
--
SELECT * FROM Northwind.dbo.[Order Details];
SELECT * FROM OrderDetails    --检查一下之前 testdb中是否用过这个表,检查后没用过,讨厌这个表还要加中括号

IF OBJECT_ID(dbo.OrderDetails,U) IS NOT NULL DROP TABLE dbo.OrderDetails;
SELECT * INTO dbo.OrderDetails FROM Northwind.dbo.[Order Details]

SELECT * FROM OrderDetails  
-------------------------------------
UPDATE dbo.OrderDetails
    SET discount = discount + 0.03
OUTPUT 
    inserted.OrderID,    
    inserted.ProductID,
    deleted.Discount AS olddiscount,
    inserted.Discount AS newdiscount
WHERE ProductID = 51;

------------------------------------

----------------------------------------------------------------------------------------------
--带有OUTPUT的MERGE语句
--此处使用之前 MERGE中 “合并的例子”
--3.Merge语句 without And condition

--如果不记得字段值,就查询一下
SELECT * FROM dbo.Customers

MERGE INTO dbo.Customers AS TGT
USING dbo.CustomersStage AS SRC
   ON TGT.custid = SRC.custid
WHEN MATCHED THEN       --此处没有其他条件,即使来源表与目标表完全相同,也要更新目标表
      UPDATE SET
           TGT.companyname = SRC.companyname,
           TGT.phone = SRC.phone,
           TGT.address = SRC.address
WHEN NOT MATCHED THEN
      INSERT (custid, companyname, phone, address)
      VALUES (SRC.custid,SRC.companyname, SRC.phone, SRC.address)  ----->>>此处,去掉MERGE之前结尾的 ; 
OUTPUT $action, inserted.custid,                                   ----->>>$action
deleted.companyname AS oldcompanyname,
inserted.companyname AS newcompanyname,
deleted.phone as oldphone,
inserted.phone as newphone,
deleted.address as oldaddress,
inserted.address as newaddress;


1) --带有OUTPUT的INSERT语句

   ......(略)

2) --带有OUTPUT的DELETE语句

......(略)
3) --带有OUTPUT的UPDATE语句
技术图片

4) --带有OUTPUT的MERGE语句

     技术图片

 

以上是关于DML_The OUTPUT Clause的主要内容,如果未能解决你的问题,请参考以下文章

python常用代码片段总结

jacript var let const 区别

Update_By_Example_Where_Clause里的代码啥意思

Android - 片段的 onActivityResult() 中的 NPE

片段内的网格适配器不起作用

Expression #14 of SELECT list is not in GROUP BY clause and contains nonaggregated column '(示例代码