mssql如何往临时表插入表数据

hjl13960 2011-09-01 05:53:27
我有1个表User(id,year,month)
我想把user表的数据插入到临时表,满足的条件如下
-----------------------------------------------------
year和month必须是最高的,也说就说是把时间最晚的插入到临时表就是了
请问我t-sql怎么写
...全文
231 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hongjian39400 的回复:]
--感觉一楼的带头大哥还靠点谱,其他的还不敢恭维
--但窃以为1楼的语句中and要改为or
[/Quote]

正确,应为 or!
hongjian39400 2011-09-01
  • 打赏
  • 举报
回复
--感觉一楼的带头大哥还靠点谱,其他的还不敢恭维
--但窃以为1楼的语句中and要改为or
czp_1030 2011-09-01
  • 打赏
  • 举报
回复

Create table #temp
(
ID nvarchar(50),
[year] nvarchar(4),
[month] nvarchar(2)
)
insert into #temp
select top 1 id,[YEAR],[month] from User order by [YEAR],[MONTH] desc
快溜 2011-09-01
  • 打赏
  • 举报
回复

--取一条加top 1
select top 1 * into #tb from [User] order by [year] desc,[month] desc

select * from #tb
快溜 2011-09-01
  • 打赏
  • 举报
回复
select *  into #tb from [User] order by [year] desc,[month] desc

select * from #tb
快溜 2011-09-01
  • 打赏
  • 举报
回复
select *  into #tb from [User] order by year desc,month desc

select * from #tb
-晴天 2011-09-01
  • 打赏
  • 举报
回复
把上面那些红色的关键字作列名的都用方括号括起来.
-晴天 2011-09-01
  • 打赏
  • 举报
回复
select * into # from user a where not exists(
select 1 from tb where year>a.year and (year=a.year and month>a.month))

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧