日期:2014-05-18  浏览次数:20380 次

在不同表里选取离当前时间点最近的7条记录,该怎么写存储过程?
假如有几个表Table1,Table2,Table3...,有相同的字段infoID,Title,PostDate(信息ID,标题,发布日期)
我要得到最新发布的7条记录,该怎么写存储过程?
create   proc   getNewInfo
@Title   varchar(60)   output,
@PostDate   smallDateTime   output
as
...

------解决方案--------------------
---try
create proc getNewInfo
as
select top 7 * from (select infoID,Title,PostDate from table1
union all
select infoID,Title,PostDate from table2
union all
select infoID,Title,PostDate from table3) a
order by PostDate desc


------解决方案--------------------
create proc getNewInfo
@Title varchar(60) output,
@PostDate smallDateTime output
as
select top 7 * from
(
select infoID,Title,PostDate from Table1
union all
select infoID,Title,PostDate from Table2
union all
select infoID,Title,PostDate from Table3
)tmp order by PostDate desc

------解决方案--------------------
select top 7 *
from
(
select infoID,Title,PostDate from Table1
union all
select infoID,Title,PostDate from Table2
union all
select infoID,Title,PostDate from Table3
)t
order by PostDate desc


------解决方案--------------------
select top 7 * from
(
select top 7 infoID,Title,PostDate from Table1
union all
select top 7 infoID,Title,PostDate from Table2
union all
select top 7 infoID,Title,PostDate from Table3
)tmp order by PostDate desc
------解决方案--------------------
create proc getNewInfo
as
select top 7 信息ID=infoID,标题=Title,发布日期=PostDate
from
(
select * from Table1
union all
select * from Table2
union all
select * from Table3
)
order by PostDate desc