SQL Server 2012上的可插入UNION ALL VIEW

时间:2016-02-22 18:28:31

标签: sql sql-server sql-view

我提供了这个架构并且不可修改:

/**
 * A class with several helper methods for working with {@link CompletionStage}
 */
public class CompletionStages {


    public static CompletionStage<Object> anyOf(CompletionStage... completionStages) {
        if (completionStages == null) {
            throw new IllegalArgumentException("You must pass a non-null argument for completionStages");
        }
        if (completionStages.length == 0) {
            throw new IllegalArgumentException("You must pass a non-empty argument for completionStages");
        }

        CompletableFuture result = new CompletableFuture();
        for(CompletionStage completionStage : completionStages) {
            completionStage.thenAccept( r -> result.complete(r));
        }
        return result;
    }


    public static CompletionStage<Void> allOf(CompletionStage... completionStages) {
        if (completionStages == null) {
            throw new IllegalArgumentException("You must pass a non-null argument for completionStages");
        }
        if (completionStages.length == 0) {
            throw new IllegalArgumentException("You must pass a non-empty argument for completionStages");
        }

        CompletionStage result = CompletableFuture.completedFuture(null);
        for(int i = 0; i < completionStages.length; i++) {
            CompletionStage curr = completionStages[i];
            result = result.thenAcceptBoth(curr, (o, o2) -> {});
        }
        return result;
    }

}

我需要一个可插入/可更新/可删除的视图,如下所示:

Table: Company1 Customers
+-------------+---------------+
| Customer ID | Customer Name |
+-------------+---------------+
| C0001       | ACME Inc.     |
| C0002       | Bogus Corp.   |
+-------------+---------------+

Table: Company2 Customers
+-------------+---------------+
| Customer ID | Customer Name |
+-------------+---------------+
| C0001       | Foo Bar Ltd.  |
| C0002       | John Smith    |
+-------------+---------------+

我把它建成:

View: All Customers
+----------+-------------+---------------+
| Company  | Customer ID | Customer Name |
+----------+-------------+---------------+
| Company1 | C0001       | ACME Inc.     |
| Company1 | C0002       | Bogus Corp.   |
| Company2 | C0001       | Foo Bar Ltd.  |
| Company2 | C0002       | John Smith    |
+----------+-------------+---------------+

如何(如果可能)我可以告诉SQL Server在CREATE VIEW [All Customers] AS SELECT 'Company1' AS [Company], [Customer ID], [Customer Name] FROM [Company1_Customers] UNION ALL SELECT 'Company2' AS [Company], [Customer ID], [Customer Name] FROM [Company2_Customers] 属于[Company] = 'Company1'Company1_Customers属于[Company] = 'Company2'的此视图中的所有INSERT / UPDATE / DELETE操作}?

2 个答案:

答案 0 :(得分:2)

您可以通过为插入/更新/删除创建INSTEAD OF触发器来完成此操作。

您可以检查inserteddeleted虚拟表以查看受影响的表,并根据此表执行正确的语句。 INSTEAD OF DELETE触发器的示例:

CREATE TRIGGER trigger_delete_all_customers ON [All Customers]
INSTEAD OF DELETE
AS
BEGIN
    DELETE c 
    FROM deleted AS d 
         INNER JOIN Company1_Customers AS c ON
              c.[Customer ID]=d.[Customer ID]
    WHERE
        d.[Company]='Company1';

    DELETE c 
    FROM deleted AS d 
         INNER JOIN Company2_Customers AS c ON
              c.[Customer ID]=d.[Customer ID]
    WHERE
        d.[Company]='Company2';
END

有关创建INSTEAD OF触发器的详细信息,请参阅documentation

答案 1 :(得分:0)

here你会看到

  

“任何修改,包括UPDATE,INSERT和DELETE语句,   必须仅从一个基表引用列。“

每个字段都有两个基表,因为你的UNION所以这个视图不能“UPDATABLE”。

您最好的办法是修复您的架构并拥有一个Customer表,其中Company为其中一列。您需要UNION完全查询的当前架构并不是最好的。