27,579
社区成员
发帖
与我相关
我的任务
分享
select 明箱 from
MAC1SN,MAC2SN,MAC3SN,MAC4SN,MAC21SN,MAC22SN,MAC23SN,MAC24SN,MAC31SN,MAC32SN,MAC33SN,MAC34SN
where 时间=(select MAX(时间)
from
(
select 时间 from MAC1SN
union select 时间 from MAC2SN
union select 时间 from MAC3SN
union select 时间 from MAC4SN
union select 时间 from MAC21SN
union select 时间 from MAC22SN
union select 时间 from MAC23SN
union select 时间 from MAC24SN
union select 时间 from MAC31SN
union select 时间 from MAC32SN
union select 时间 from MAC33SN
union select 时间 from MAC34SN)
temp
)
IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t
CREATE TABLE #t(ID INT IDENTITY(1,1),TableName NVARCHAR(500), 时间 datetime,明箱 nvarchar(1000))
insert into #t(TableName,时间,明箱)
EXEC sys.sp_MSforeachtable @command1=' select top 1 PARSENAME(''?'',1), 时间,明箱 from ? order by 时间 DESC '
,@whereand=' and charindex('',''+ object_name(object_id)+ '','','',MAC1SN,MAC2SN,MAC3SN,MAC4SN,MAC21SN,MAC22SN,MAC23SN,MAC24SN,MAC31SN,MAC32SN,MAC33SN,MAC34SN,'')>0'
SELECT TOP 1 * FROM #t ORDER BY 时间 DESC
with t as(
select 明箱,时间 from [表1]
union all
select 明箱,时间 from [表2]
union all
select 明箱,时间 from [表3]
union all
select 明箱,时间 from [表4]
union all
select 明箱,时间 from [表5]
union all
select 明箱,时间 from [表6]
union all
select 明箱,时间 from [表7]
union all
select 明箱,时间 from [表8])
select 明箱
from t
where 时间=(select max(时间) from t)