通过视图 sql 服务器在相关表中插入数据
insert data in related tables through a view sql server
希望有人可以帮助解决这个问题。我有两个 tables(结构如下):
Table1
DataYear INT Not Null,
ProvId INT Not Null,
LKId INT Not Null,
UpdateTime Datetime2(2) Not Null,
JTimes INT Not Null
Table2
DataYear INT Not Null,
ProvId INT Not Null,
LKId INT Not Null,
UpdateTime Datetime2(2) Not Null,
IsSuspect BIT,
Recs VARCHAR(10)
两个 table 的 PK 是前 4 列。该视图将采用 Table 1 中的所有值和 table 2 中的最后两列。
我想在视图中插入数据并填充两个表中的所有列。这可能吗?我该怎么做?
希望有人能帮忙
谢谢
亚历克斯
您可以在视图上使用 INSEAD OF INSERT
触发器将数据插入多个表。
CREATE TABLE dbo.Table1
(
DataYear int NOT NULL
, ProvId int NOT NULL
, LKId int NOT NULL
, UpdateTime datetime2(2) NOT NULL
, JTimes int NOT NULL
, CONSTRAINT PK_Table1 PRIMARY KEY(
DataYear
, ProvId
, LKId
, UpdateTime)
);
CREATE TABLE dbo.Table2
(
DataYear int NOT NULL
, ProvId int NOT NULL
, LKId int NOT NULL
, UpdateTime datetime2(2) NOT NULL
, IsSuspect bit
, Recs varchar(10)
, CONSTRAINT PK_Table2 PRIMARY KEY(
DataYear
, ProvId
, LKId
, UpdateTime)
);
GO
CREATE VIEW dbo.View1 AS
SELECT
Table1.DataYear
, Table1.ProvId
, Table1.LKId
, Table1.UpdateTime
, Table1.JTimes
, Table2.IsSuspect
, Table2.Recs
FROM dbo.Table1
JOIN dbo.Table2 ON
Table2.DataYear = Table1.DataYear
AND Table2.ProvId = Table1.ProvId
AND Table2.LKId = Table1.LKId
AND Table2.UpdateTime = Table1.UpdateTime
GO
CREATE TRIGGER INSTEAD_OF_INSERT_View1
ON dbo.View1 INSTEAD OF INSERT AS
INSERT INTO dbo.Table1(
DataYear
, ProvId
, LKId
, UpdateTime
, JTimes)
SELECT
DataYear
, ProvId
, LKId
, UpdateTime
, JTimes
FROM inserted;
INSERT INTO dbo.Table2(
DataYear
, ProvId
, LKId
, UpdateTime
, IsSuspect
, Recs)
SELECT
DataYear
, ProvId
, LKId
, UpdateTime
, IsSuspect
, Recs
FROM inserted;
GO
INSERT INTO dbo.View1
( DataYear
, ProvId
, LKId
, UpdateTime
, JTimes
, IsSuspect
, Recs
)
VALUES ( 1
, 1
, 1
, SYSDATETIME()
, 1
, 1
, '1'
);
GO
希望有人可以帮助解决这个问题。我有两个 tables(结构如下):
Table1
DataYear INT Not Null,
ProvId INT Not Null,
LKId INT Not Null,
UpdateTime Datetime2(2) Not Null,
JTimes INT Not Null
Table2
DataYear INT Not Null,
ProvId INT Not Null,
LKId INT Not Null,
UpdateTime Datetime2(2) Not Null,
IsSuspect BIT,
Recs VARCHAR(10)
两个 table 的 PK 是前 4 列。该视图将采用 Table 1 中的所有值和 table 2 中的最后两列。
我想在视图中插入数据并填充两个表中的所有列。这可能吗?我该怎么做?
希望有人能帮忙
谢谢
亚历克斯
您可以在视图上使用 INSEAD OF INSERT
触发器将数据插入多个表。
CREATE TABLE dbo.Table1
(
DataYear int NOT NULL
, ProvId int NOT NULL
, LKId int NOT NULL
, UpdateTime datetime2(2) NOT NULL
, JTimes int NOT NULL
, CONSTRAINT PK_Table1 PRIMARY KEY(
DataYear
, ProvId
, LKId
, UpdateTime)
);
CREATE TABLE dbo.Table2
(
DataYear int NOT NULL
, ProvId int NOT NULL
, LKId int NOT NULL
, UpdateTime datetime2(2) NOT NULL
, IsSuspect bit
, Recs varchar(10)
, CONSTRAINT PK_Table2 PRIMARY KEY(
DataYear
, ProvId
, LKId
, UpdateTime)
);
GO
CREATE VIEW dbo.View1 AS
SELECT
Table1.DataYear
, Table1.ProvId
, Table1.LKId
, Table1.UpdateTime
, Table1.JTimes
, Table2.IsSuspect
, Table2.Recs
FROM dbo.Table1
JOIN dbo.Table2 ON
Table2.DataYear = Table1.DataYear
AND Table2.ProvId = Table1.ProvId
AND Table2.LKId = Table1.LKId
AND Table2.UpdateTime = Table1.UpdateTime
GO
CREATE TRIGGER INSTEAD_OF_INSERT_View1
ON dbo.View1 INSTEAD OF INSERT AS
INSERT INTO dbo.Table1(
DataYear
, ProvId
, LKId
, UpdateTime
, JTimes)
SELECT
DataYear
, ProvId
, LKId
, UpdateTime
, JTimes
FROM inserted;
INSERT INTO dbo.Table2(
DataYear
, ProvId
, LKId
, UpdateTime
, IsSuspect
, Recs)
SELECT
DataYear
, ProvId
, LKId
, UpdateTime
, IsSuspect
, Recs
FROM inserted;
GO
INSERT INTO dbo.View1
( DataYear
, ProvId
, LKId
, UpdateTime
, JTimes
, IsSuspect
, Recs
)
VALUES ( 1
, 1
, 1
, SYSDATETIME()
, 1
, 1
, '1'
);
GO