首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

在不同表里选取离当前时间点最近的7条记录,该如何写存储过程

2012-02-26 
在不同表里选取离当前时间点最近的7条记录,该怎么写存储过程?假如有几个表Table1,Table2,Table3...,有相同

在不同表里选取离当前时间点最近的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

热点排行