每隔N行就插入一空行?
有如下表:
tb_Test
c_No
123
124
125
126
127
128
129
133
134
135
现在要每隔3行就插入一空行,得到如下结果:
123
124
125
126
127
128
129
133
134
135
应该如何做?
问题点数:50、回复次数:10Top
1 楼fanweiwei(黑暗凝聚力量,堕落方能自由)回复于 2006-03-16 11:14:03 得分 0
数据库中这样做有什么用处啊Top
2 楼insert2003(高级打字员)回复于 2006-03-16 11:15:42 得分 0
为了显示的需要!Top
3 楼lsqkeke(可可)回复于 2006-03-16 11:37:10 得分 10
数据库中记录的存储顺序是由聚集索引决定的!
如果有聚集索引,用select * from tb 查询出来的数据就是存储顺序!
如果表没有聚集索引,也没有其它索引,那么显示的记录顺序是不定的!
即 : 即使你每三条记录后插入了空记录行,显示时也未必就是你要的顺序!
不用在数据库中记录空记录,你显示时再处理吧!
但这个要求是有点难的
Top
4 楼ping3000(苦练葵花点穴手)回复于 2006-03-16 11:47:11 得分 10
如果为了显示最好在客户端做
可以用临时表存放数据,再把tb_Test表里的数据删除,然后把数据倒过来
create table tb_Test(c_No nvarchar(50))
insert into tb_Test(c_No)
select '123' union all
select '124' union all
select '125' union all
select '126' union all
select '127' union all
select '128' union all
select '129' union all
select '133' union all
select '134' union all
select '135'
create table tb_Test1(C nvarchar(50))
declare @a nvarchar(50)
declare @i int
set @i = -1
declare cp_cursor cursor for select c_No from tb_Test
open cp_cursor
fetch next from cp_cursor into @a
while @@FETCH_STATUS = 0
begin
set @i = @i + 1
if @i = 3
begin
insert into tb_Test1(C) values('')
set @i = 0
end
Insert Into tb_Test1(C) select @a
fetch next from cp_Cursor into @a
END
close cp_Cursor
deallocate cp_Cursor
select * from tb_Test1
drop table tb_Test
drop table tb_Test1Top
5 楼mzh_mcc(技术升级中……)回复于 2006-03-16 11:56:11 得分 0
应该在客户端显示的时候做,这样在数据库里插入空行不符合数据库设计的范式。Top
6 楼gaojier1000(V2※高捷)回复于 2006-03-16 12:03:56 得分 0
显示的时候处理吧,不要在数据库里面出现这样的数据!Top
7 楼zjdyzwx(十一月猪)回复于 2006-03-16 12:43:55 得分 10
DECLARE @T TABLE(c_NO VARCHAR(3))
DECLARE @T1 TABLE(c_NO VARCHAR(3))
INSERT INTO @T
SELECT '123' UNION
SELECT '124' UNION
SELECT '125' UNION
SELECT '126' UNION
SELECT '127' UNION
SELECT '128' UNION
SELECT '129' UNION
SELECT '133' UNION
SELECT '134' UNION
SELECT '135'
DECLARE @I INT
DECLARE @I1 VARCHAR(8)
SET @I = 1
DECLARE CURSOR1 CURSOR SCROLL FOR SELECT * FROM @T
OPEN CURSOR1
FETCH NEXT FROM CURSOR1 INTO @I1
WHILE(@@FETCH_STATUS = 0)
BEGIN
SET @I = @I + 1
INSERT INTO @T1
SELECT @I1
IF (@I%3 = 0)
INSERT INTO @T1
SELECT ''
FETCH NEXT FROM CURSOR1 INTO @I1
END
CLOSE CURSOR1
DEALLOCATE CURSOR1
SELECT * FROM @T1
WHERE 1 = 1
Top
8 楼vovo2000(没人要的猫)回复于 2006-03-16 13:13:32 得分 0
可以应用程序端的显示程序中实现。在数据库中实现不太好。Top
9 楼zhaoanle(zhao)回复于 2006-03-16 13:51:00 得分 20
--测试数据
create table tb_test(c_no int)
insert tb_test select 123
insert tb_test select 124
insert tb_test select 125
insert tb_test select 126
insert tb_test select 127
insert tb_test select 128
insert tb_test select 129
insert tb_test select 133
insert tb_test select 134
insert tb_test select 135
create table tb_test1(c_no int) --建转换后的表
--查询
declare @cnt int
declare @i int
declare @sql varchar(8000)
select @cnt=count(*) from tb_test
if @cnt<3
insert tb_test1 select * from tb_test
else
begin
insert tb_test1 select top 3 * from tb_test
insert tb_test1 values(null)
set @i=3
while @cnt-@i>-2
begin
set @sql='insert tb_test1 select top 3 * from tb_test where c_no not in (select top '+cast(@i as varchar(10))+ ' c_no from tb_test)'
exec(@sql)
insert tb_test1 values(null)
set @i=@i+3
end
end
select * from tb_test1
--删除测试数据
drop table tb_test1
drop table tb_test
/*结果
c_no
-----------
123
124
125
NULL
126
127
128
NULL
129
133
134
NULL
135
NULL
(所影响的行数为 14 行)
*/
Top
10 楼lcooc(don't make me think)回复于 2006-03-16 14:06:41 得分 0
你可以在程序中判断,
如果有3行,输出一个空行Top




