当前位置: 代码迷 >> Sql Server >> union all視圖數據的操作解决思路
  详细解决方案

union all視圖數據的操作解决思路

热度:9   发布时间:2016-04-27 15:29:00.0
union all視圖數據的操作
建立三個數據表(stock_2005,stock_2006,stock_2007)各數據表操作都有條件約束.

CREATE TABLE [dbo].[stock_2005] (
[mat_no] [varchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[years] [varchar] (4) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[mat_qty] [decimal](18, 2) NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[stock_2006] (
[mat_no] [varchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[years] [varchar] (4) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[mat_qty] [decimal](18, 2) NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[stock_2007] (
[mat_no] [varchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[years] [varchar] (4) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[mat_qty] [decimal](18, 2) NULL 
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[stock_2005] WITH NOCHECK ADD 
CONSTRAINT [PK_stock_2005] PRIMARY KEY CLUSTERED 
(
[mat_no],
[years]
) ON [PRIMARY] 
GO

ALTER TABLE [dbo].[stock_2006] WITH NOCHECK ADD 
CONSTRAINT [PK_stock_2006] PRIMARY KEY CLUSTERED 
(
[mat_no],
[years]
) ON [PRIMARY] 
GO

ALTER TABLE [dbo].[stock_2007] WITH NOCHECK ADD 
CONSTRAINT [PK_stock_2007] PRIMARY KEY CLUSTERED 
(
[mat_no],
[years]
) ON [PRIMARY] 
GO

ALTER TABLE [dbo].[stock_2005] ADD 
CONSTRAINT [DF_stock_2005_mat_qty] DEFAULT (0) FOR [mat_qty],
CONSTRAINT [CK_stock_2005] CHECK ([years] = 2005)
GO

ALTER TABLE [dbo].[stock_2006] ADD 
CONSTRAINT [DF_stock_2006_mat_qty] DEFAULT (0) FOR [mat_qty],
CONSTRAINT [CK_stock_2006] CHECK ([years] = 2006)
GO

ALTER TABLE [dbo].[stock_2007] ADD 
CONSTRAINT [DF_stock_2007_mat_qty] DEFAULT (0) FOR [mat_qty],
CONSTRAINT [CK_stock_2007] CHECK ([years] = 2007)
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

現建立視圖stock
select * from stock_2005
union all 
select * from stock_2006
union all
select * from stock_2007

請問可否對視圖stock進行新增,修改,刪除操作?可以的,要如何操作. 


------解决方案--------------------
在视图里建instead of update--触发器就可实现视图更新