Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

MERGE कथन की लक्ष्य तालिका में सक्षम नियमों की अनुमति क्यों नहीं है?

मर्ज उदाहरण (नियमों के साथ और CHECK . के साथ बाधाएं):

CREATE RULE MyRule
AS 
@Status IN ('Y', 'N');
GO

CREATE TABLE dbo.SalesOrder
(
    SalesOrderID INT PRIMARY KEY
    ,OrderDate DATETIME NOT NULL
    ,IsDeleted CHAR(1) NOT NULL DEFAULT 'N'
);
GO

EXEC sp_bindrule @rulename='MyRule', @objname='dbo.SalesOrder.IsDeleted';
GO

INSERT  dbo.SalesOrder (SalesOrderID, OrderDate)
SELECT  1, '20110101'
UNION ALL
SELECT  2, '20110202'
UNION ALL
SELECT  3, '20110303';
GO

SELECT  *
FROM    dbo.SalesOrder;

PRINT '*****First test*****';
GO

MERGE   dbo.SalesOrder Dst
USING   (VALUES (1,'Y'), (4,'Y')) AS Src(SalesOrderID, IsDeleted) 
ON      Dst.SalesOrderID = Src.SalesOrderID
WHEN    MATCHED THEN UPDATE SET IsDeleted = Src.IsDeleted
WHEN    NOT MATCHED BY TARGET THEN INSERT (SalesOrderID, OrderDate, IsDeleted) VALUES (Src.SalesOrderID, GETDATE(), Src.IsDeleted);
GO

EXEC sp_unbindrule 'dbo.SalesOrder.IsDeleted'; --Disabling `MyRule` for IsDeleted column
ALTER TABLE dbo.SalesOrder --We "replace" the old rule with a new `CHECK` constraint
ADD CONSTRAINT CK_SalesOrder_IsDeleted CHECK( IsDeleted IN ('Y', 'N') );
GO

PRINT '*****Second test*****';
MERGE   dbo.SalesOrder Dst
USING   (VALUES (1,'Y'), (4,'Y')) AS Src(SalesOrderID, IsDeleted) 
ON      Dst.SalesOrderID = Src.SalesOrderID
WHEN    MATCHED THEN UPDATE SET IsDeleted = Src.IsDeleted
WHEN    NOT MATCHED BY TARGET THEN INSERT (SalesOrderID, OrderDate, IsDeleted) VALUES (Src.SalesOrderID, GETDATE(), Src.IsDeleted);
GO

SELECT  *
FROM    dbo.SalesOrder;

DROP TABLE dbo.SalesOrder;
DROP RULE MyRule;

परिणाम:

Rule bound to table column.

(3 row(s) affected)
SalesOrderID OrderDate               IsDeleted
------------ ----------------------- ---------
1            2011-01-01 00:00:00.000 N
2            2011-02-02 00:00:00.000 N
3            2011-03-03 00:00:00.000 N

(3 row(s) affected)

*****First test*****
Msg 358, Level 16, State 1, Line 2
The target table 'Dst' of the MERGE statement cannot have any enabled rules.  Found rule 'MyRule'.
Rule unbound from table column.
*****Second test*****

(2 row(s) affected)
SalesOrderID OrderDate               IsDeleted
------------ ----------------------- ---------
1            2011-01-01 00:00:00.000 Y
2            2011-02-02 00:00:00.000 N
3            2011-03-03 00:00:00.000 N
4            2011-09-20 16:03:56.030 Y

(4 row(s) affected)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. किसी दिए गए संग्रहीत-खरीद को कॉल करने वाले सभी SQL एजेंट जॉब्स को कैसे खोजें

  2. तालिका सामग्री को पॉप अप फॉर्म में स्वचालित रूप से कैसे दिखाया जाए?

  3. SQL सर्वर (T-SQL) से ईमेल कैसे भेजें

  4. उच्चतम भुगतान वाली कर्मचारी पंक्ति कैसे प्राप्त करें

  5. कीवर्ड समर्थित नहीं:मेटाडेटा