转自:http://www.maomao365.com/?p=6679
摘要:
下文将分享使用sql脚本输出交替变换的不同背景颜色的sql脚本的方法分享,如下所示:
实验环境:sqlserver 2008 R2
例:
下文 首先采用 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