如何把查询出来的结果集插入到临时表?

sdnwjd 2009-12-04 09:09:21
已经查询出来的结果集是:

select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
order by t.xm_id

-----------------------------------------------------------------------------------------------------------
我想上面的结果集插入到临时表#test1中,把这个结果集的字段作为#test1的字段,但是提示出错

错误信息为:服务器: 消息 1033,级别 15,状态 1,行 22
除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
insert into #test1 select

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
order by t.xm_id
)

...全文
791 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdnwjd 2009-12-04
  • 打赏
  • 举报
回复
谢谢大家啊


先出去了,一会回来结贴
sdnwjd 2009-12-04
  • 打赏
  • 举报
回复
明白了

这么写就对了:


select * into #test1 from

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'


)m


select * from #test1 order by 项目编号
drop table #test1
叶子 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sdnwjd 的回复:]
insert into #test1 select

(
select 业务编号=t.yw_id,
      项目编号 = t.xm_id,
      当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
      累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
      日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
) m
order by xm_id

---------------------------------------------------------------------------
select * from #test1

服务器: 消息 208,级别 16,状态 1,行 1
对象名 '#test1' 无效。

怎么提示临时表#test1无效,我想查询出#test1的数据
[/Quote]

你直接insert into #test1 ,但是还没有创建#test1 ,所以不行。

用select * into #test1 from 是直接创建后插入,两种写法不一样。
叶子 2009-12-04
  • 打赏
  • 举报
回复
对象名 '#test1' 无效。

说明你上面的插入失败了。
banana_97 2009-12-04
  • 打赏
  • 举报
回复
select * into #test1(這里才是臨時表啊)
insert into #test1 這里是表啊
sych888 2009-12-04
  • 打赏
  • 举报
回复
SELECT ... INTO # FROM ...
sdnwjd 2009-12-04
  • 打赏
  • 举报
回复
insert into #test1 select

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
) m
order by xm_id

---------------------------------------------------------------------------
select * from #test1

服务器: 消息 208,级别 16,状态 1,行 1
对象名 '#test1' 无效。

怎么提示临时表#test1无效,我想查询出#test1的数据
xuejie09242 2009-12-04
  • 打赏
  • 举报
回复
select 后加 top 100 percent
就可以了,还是全部的记录。
直接用select into 好了。
select .... into #test1 from .....
nianran520 2009-12-04
  • 打赏
  • 举报
回复

insert into #test1
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
order by t.xm_id

qiqi860819 2009-12-04
  • 打赏
  • 举报
回复
select top 100 percent
dawugui 2009-12-04
  • 打赏
  • 举报
回复
insert into #test1 select 

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
) m
order by xm_id
--小F-- 2009-12-04
  • 打赏
  • 举报
回复
貌似掉了个别名
insert into #test1 select

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'

) t
order by t.xm_id
华夏小卒 2009-12-04
  • 打赏
  • 举报
回复
select 业务编号=t.yw_id, 
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
into #test1
from t_bb2_xm t where t.status='启用' and yw_id='收入'
order by t.xm_id
黄_瓜 2009-12-04
  • 打赏
  • 举报
回复
提示很明显了,不要order by
存的时候干没有必要排序
--小F-- 2009-12-04
  • 打赏
  • 举报
回复
insert into #test1 select 

(
select
top 8000
业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'
order by t.xm_id
)
--小F-- 2009-12-04
  • 打赏
  • 举报
回复
insert into #test1 select 

(
select 业务编号=t.yw_id,
项目编号 = t.xm_id,
当日收入金额 = isnull((select dr from t_bb2_in where xm_id = t.xm_id and rq ='2009-11-1' and dept_id='屋'),0.00) ,
累计收入金额 = isnull((select sum(dr) from t_bb2_in where xm_id = t.xm_id and (rq>='2009-11-1' and rq <= '2009-11-2') and dept_id='屋'),0.00),
日期 = '2009-11-2'
from t_bb2_xm t where t.status='启用' and yw_id='收入'

)
order by t.xm_id

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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