sql server 视图添加一列

chestnut 2013-08-27 02:53:27
有个视图,想在视图中增加一列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;

是想在建视图的时候添加一列,不是在基础表添加一列!!!
...全文
2512 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chestnut 2013-08-28
  • 打赏
  • 举报
回复
引用 8 楼 wwwwgou 的回复:
[quote=引用 5 楼 journeysxy 的回复:] [quote=引用 3 楼 rockyljt 的回复:]
ALTER view vtest as select id=ROW_NUMBER() OVER(PARTITION BY GETDATE() ORDER BY city),city,code from test
当数据量大的时候,我在原来是视图的上查询数据1000条数10s,但是加了id后的查询1000条数据用来1分钟!!![/quote] 所以说,一般建表,在没有合适主键的情况下,都会给表加一个自增列的主键。 像你这样的需求,没有什么高效的办法解决。给表加一列: alter table tb add id int not null identity(1,1) [/quote]真忧伤...
yinsuxia 2013-08-28
  • 打赏
  • 举报
回复
用row_number()就行
---涛声依旧--- 2013-08-27
  • 打赏
  • 举报
回复
建立表时,最好有一个自增列id或GUID,并建立唯一索引
Shawn 2013-08-27
  • 打赏
  • 举报
回复
引用 5 楼 journeysxy 的回复:
[quote=引用 3 楼 rockyljt 的回复:]
ALTER view vtest as select id=ROW_NUMBER() OVER(PARTITION BY GETDATE() ORDER BY city),city,code from test
当数据量大的时候,我在原来是视图的上查询数据1000条数10s,但是加了id后的查询1000条数据用来1分钟!!![/quote] 所以说,一般建表,在没有合适主键的情况下,都会给表加一个自增列的主键。 像你这样的需求,没有什么高效的办法解决。给表加一列: alter table tb add id int not null identity(1,1)
Shawn 2013-08-27
  • 打赏
  • 举报
回复
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
/*
id	city	code
1	广州	1111A
2	深圳	123A
3	厦门	159A
*/
chestnut 2013-08-27
  • 打赏
  • 举报
回复
引用 2 楼 TravyLee 的回复:

ALTER view [dbo].vtest
as
select id=ROW_NUMBER()over(order by getdate()),* from test

GO
当数据量大的时候,我在原来是视图的上查询数据1000条数10s,但是加了id后的查询1000条数据用来1分钟!!!
chestnut 2013-08-27
  • 打赏
  • 举报
回复
引用 3 楼 rockyljt 的回复:
ALTER view vtest as select id=ROW_NUMBER() OVER(PARTITION BY GETDATE() ORDER BY city),city,code from test
当数据量大的时候,我在原来是视图的上查询数据1000条数10s,但是加了id后的查询1000条数据用来1分钟!!!
---涛声依旧--- 2013-08-27
  • 打赏
  • 举报
回复
结果: id city code 1 广州 1111A 2 深圳 123A 3 厦门 159A
---涛声依旧--- 2013-08-27
  • 打赏
  • 举报
回复
ALTER view vtest as select id=ROW_NUMBER() OVER(PARTITION BY GETDATE() ORDER BY city),city,code from test
  • 打赏
  • 举报
回复

ALTER view [dbo].vtest
as
select id=ROW_NUMBER()over(order by getdate()),* from test

GO
---涛声依旧--- 2013-08-27
  • 打赏
  • 举报
回复
楼主用的是sql2005以上版本吗?如果是的话可以借助row_number()

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧