日期:2014-05-17  浏览次数:20839 次

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