We generally seek the maximum value for a column, but occasionally seek the maximum value in each row, introduce today, the maximum value of each row wording
--测试数据
if not object_id(N'T') is null
drop table T
Go
Create table T([id1] int,[id2] int,[id3] int)
Insert T
select 1,2,3 union all
select 9,8,7
Go
--测试数据结束
DECLARE @sql NVARCHAR(max)='select (select Max(MaxValue) from (values '
SELECT
@sql = @sql + STUFF(
(
SELECT
',(' + name + ')'
FROM
syscolumns
WHERE
id =
(
SELECT
MAX(id)
FROM
sysobjects
WHERE
xtype = 'u'
AND name = 'T' --表名
)
FOR XML PATH('')
), 1, 1, ''
);
SET @sql=@sql+') as #temp(MaxValue)) as MaxValue from T'
EXEC(@sql)
result:
We can see the maximum two lines are displayed out.