一段sql语句access view
希望能对大家有所启发!
create table trade_01(
tradeid uniqueidentifier not null default(newid() ),
tradedate datetime not null
)
go
alter table table_01 add constraint pk_trade_01 primary key nonclustered(tradeid)
alter table table_01 add constraint ck_trade_01 check(month(tradedate)=1 and year(tradedate)=2000)
go
create table trade_02(
tradeid uniqueidentifier not null default(newid() ),
tradedate datetime not null
)
go
alter table table_02 add constraint pk_trade_02 primary key nonclustered(tradeid)
alter table table_02 add constraint ck_trade_02 check(month(tradedate)=2 and year(tradedate)=2000)
go
insert trade_01 (tradedate) values('1 jan 2000')
insert trade_02 (tradedate) values('1 feb 2000')
go
create view v_trades
as
select * from trade_01
union all
select * from trade_02
go
set showplan_text on
go
select *
from v_trades
where tradedate='1 feb 2000'
go
set showplan_text off
go
alter table trade_01 add partcol int not null default(1)
go
alter table trade_01 add constraint ck_trade_01works check(partcol=1)
go
alter table trade_02 add partcol int not null default(2)
go
alter table trade_02 add constraint ck_trade_02works check(partcol=2)
go
alter view v_trades
as
select * from trade_01
union all
select * from trade_02
go
set showplan_text on
go
select *
from v_trades
where partcol=2
go
set showplan_text off
go
drop table trade_01
drop table trade_02
drop view v_trades
go
create table trade_01(
tradeid uniqueidentifier not null default(newid() ),
tradedate datetime not null
)
go
alter table table_01 add constraint pk_trade_01 primary key nonclustered(tradeid)
alter table table_01 add constraint ck_trade_01 check(month(tradedate)=1 and year(tradedate)=2000)
go
create table trade_02(
tradeid uniqueidentifier not null default(newid() ),
tradedate datetime not null
)
go
alter table table_02 add constraint pk_trade_02 primary key nonclustered(tradeid)
alter table table_02 add constraint ck_trade_02 check(month(tradedate)=2 and year(tradedate)=2000)
go
insert trade_01 (tradedate) values('1 jan 2000')
insert trade_02 (tradedate) values('1 feb 2000')
go
create view v_trades
as
select * from trade_01
union all
select * from trade_02
go
set showplan_text on
go
select *
from v_trades
where tradedate='1 feb 2000'
go
set showplan_text off
go
alter table trade_01 add partcol int not null default(1)
go
alter table trade_01 add constraint ck_trade_01works check(partcol=1)
go
alter table trade_02 add partcol int not null default(2)
go
alter table trade_02 add constraint ck_trade_02works check(partcol=2)
go
alter view v_trades
as
select * from trade_01
union all
select * from trade_02
go
set showplan_text on
go
select *
from v_trades
where partcol=2
go
set showplan_text off
go
drop table trade_01
drop table trade_02
drop view v_trades
go
juboy