sql server 视图添加一列
有个视图,想在视图中增加一列id,自动增长。原本视图数据是:
city code
广州 1111A
深圳 123A
厦门 159A
实现效果是:
id city code
1 广州 1111A
2 深圳 123A
3 厦门 159A
原来视图的脚本是:
if exists (select * from sys.objects where name ='test')
drop table test;
create table test (city varchar(10),code varchar(10))
insert into test
select '广州','1111A' union all
select '深圳', '123A' union all
select '厦门', '159A'
if exists (select * from sys.objects where name ='vtest')
drop view vtest;
create view vtest as select * from test;
是想在建视图的时候添加一列,不是在基础表添加一列!!! sql?server 脚本 视图
[解决办法]
ALTER view [dbo].vtest
as
select id=ROW_NUMBER()over(order by getdate()),* from test
GO
ALTER view vtest as select id=ROW_NUMBER() OVER(PARTITION BY GETDATE() ORDER BY city),city,code from test
if exists (select * from sys.objects where name ='test')
drop table test;
GO
create table test (city varchar(10),code varchar(10))
insert into test
select '广州','1111A' union all
select '深圳', '123A' union all
select '厦门', '159A'
GO
if exists (select * from sys.objects where name ='vtest')
drop view vtest;
GO
create view vtest as select id=ROW_NUMBER() OVER(ORDER BY GETDATE()),* from test;
GO
SELECT *FROM vtest
/*
idcitycode
1广州1111A
2深圳123A
3厦门159A
*/