有表test
create table test
(
id int primary key identity(1,1),
name varchar(10)
)
创建一个视图test_view
create view test_view as select * from test where name like '%张%'
请教:
创建后,视图的id怎么能够重新再顺序的按1,2,3,4,5,6,7,8,9,10这样排序,而不是有间断。
------解决方案--------------------
- SQL code
create view test_view as select id=row_number over(order by getdate()),name from test where name like '%张%'
------解决方案--------------------
这就不需要建立视图了,用临时表。
select *,rid=identity(int,1,1)
into #tb
from tb
where name like '%张%'
select * from #tb
看看。
------解决方案--------------------
- SQL code
select row_number() over(order by id) as id,namefrom test_vieworder by id
------解决方案--------------------
- SQL code
create view test_view as select ID=row_number()over(order by ID ), [name]from test where name like '%张%'
------解决方案--------------------
2楼是05写法,不过想问楼主为何要这样子去做。
------解决方案--------------------
你不是有自增列吗?id不按照1,2,3,4,5,6,7,8,9,10这样排序?
------解决方案--------------------
注意到2K
- SQL code
select (select count(*) from test_view where id <= a.id) as id,namefrom test_view aorder by id
------解决方案--------------------
- SQL code
难道是select row_number() over(order by id) as id ,name from test_view order by id
------解决方案--------------------
... 2000的直接看7楼。
------解决方案--------------------
2楼的用临时表实现可以的,楼主试试