下文 首先利用 over() row

 

转自:http://www.maomao365.com/?p=6679

摘要:

下文将享用应用sql脚本输出交替转换的两样背景颜色的sql脚本的法门分享,如下所示:
尝试碰着:sqlserver 二〇一〇 福睿斯2


例:
下文 首先使用 over() row_number 函数生成的行编号,
下一场对每行进行颜色变化操作,生成差异的背景象,如下所示:

create table test(keyId int,info varchar(30))
go
insert into test(keyId,info)values(10,'测试信息20180625-1')
insert into test(keyId,info)values(20,'测试信息20180626-2')
insert into test(keyId,info)values(21,'测试信息20180628-3')
insert into test(keyId,info)values(81,'测试信息20180620-4')
insert into test(keyId,info)values(92,'测试信息20180608-5')
insert into test(keyId,info)values(101,'测试信息20180605-6')
insert into test(keyId,info)values(102,'测试信息20180606-7')
go


declare @tmp varchar(max)
set @tmp ='<table>'
set @tmp =@tmp+'<tr><td>流水号<td>keyId<td>info</tr>'

select 
@tmp=@tmp+'<tr style=''background-color:'+ case when t.[编号] %2=0 then 'blue' else '' end+'''>'
+'<td>'+ convert(varchar(100),t.[编号])
+'<td>'+ convert(varchar(100),t.keyId)
+'<td>'+t.info
+'</tr>'
from 
(
select row_number() over(order by keyId asc ) as [编号],
keyId,info from test ) as t 


set @tmp =@tmp+'</table>'
select @tmp ---打印生成的html信息 

go
drop table test 

 

相关文章