求 DBA_HuangZJ(发粪涂墙) 师傅在看看

lu仙深 2014-08-13 01:24:56
能否把最后返回的记录放在临时表中

最后的结果我想 SELECT * FROM #XXX 查询出来,
放在临时表中我可以继续加工下,非常感谢



----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-05-26 17:37:39
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[商品表]
if object_id('商品表') is not null drop table 商品表
go
create table 商品表([商品ID] int,[商品名称] nvarchar(4))
insert 商品表
select 101,N'花生' union all
select 102,N'啤酒'union all
select 103,N'毛线'
--> 测试数据[仓库表]
if object_id('仓库表') is not null drop table 仓库表
go
create table 仓库表([仓库ID] int,[仓库名称] nvarchar(4))
insert 仓库表
select 201,N'仓1' union all
select 202,N'仓2'
--> 测试数据[库存表]
if object_id('库存表') is not null drop table 库存表
go
create table 库存表([商品ID] int,[仓库ID] int,[商品数量] int)
insert 库存表
select 101,201,100 union all
select 101,202,50 union all
select 102,201,80
--------------生成数据--------------------------
IF OBJECT_ID('TempDB..#t','u')IS NOT NULL
DROP TABLE #t

select c.商品ID ,c.商品名称,ISNULL(b.仓库名称,'仓1')仓库名称,ISNULL(a.商品数量,0)商品数量 INTO #t
from 库存表 a left JOIN 仓库表 b ON a.仓库ID=b.仓库ID
right JOIN 商品表 c ON a.商品ID=c.商品ID

declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename(仓库名称)+N'=sum(case when [仓库名称]=N'+quotename(仓库名称,'''')+N' then [商品数量] else 0 end)'
from #t group by 仓库名称
exec(N'select [商品ID],商品名称'+@s+N',[合计]=isnull(sum(商品数量),0) from #t group by [商品ID],商品名称 order by 商品ID')

----------------结果----------------------------
/*
商品ID 商品名称 仓1 仓2 合计
----------- ---- ----------- ----------- -----------
101 花生 100 50 150
102 啤酒 80 0 80
103 毛线 0 0 0

*/



...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lu仙深 2014-08-13
  • 打赏
  • 举报
回复
谢谢,搞定了,## 全局的才行,我终于明白了,,我自己改造了下写成了固定的 临时表了 3Q
發糞塗牆 2014-08-13
  • 打赏
  • 举报
回复
动态凭借一般是最终结果,作为中间结果不好操作
lu仙深 2014-08-13
  • 打赏
  • 举报
回复
發糞塗牆 2014-08-13
  • 打赏
  • 举报
回复
目前我向导的是这样,但是有个问题,在你最终运行之前,你不知道表名具体是什么


----------------------------------------------------------------
-- Author  :DBA_HuangZJ(发粪涂墙)
-- Date    :2014-05-26 17:37:39
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--	Apr  2 2010 15:48:46 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[商品表]
if object_id('商品表') is not null drop table 商品表
go 
create table 商品表([商品ID] int,[商品名称] nvarchar(4))
insert 商品表
select 101,N'花生' union all
select 102,N'啤酒'union all
select 103,N'毛线'
--> 测试数据[仓库表]
if object_id('仓库表') is not null drop table 仓库表
go 
create table 仓库表([仓库ID] int,[仓库名称] nvarchar(4))
insert 仓库表
select 201,N'仓1' union all
select 202,N'仓2'
--> 测试数据[库存表]
if object_id('库存表') is not null drop table 库存表
go 
create table 库存表([商品ID] int,[仓库ID] int,[商品数量] int)
insert 库存表
select 101,201,100 union all
select 101,202,50 union all
select 102,201,80
--------------生成数据--------------------------
IF OBJECT_ID('TempDB..#t','u')IS NOT NULL 
DROP TABLE #t

select c.商品ID ,c.商品名称,ISNULL(b.仓库名称,'仓1')仓库名称,ISNULL(a.商品数量,0)商品数量 INTO #t
from 库存表 a left JOIN 仓库表 b ON a.仓库ID=b.仓库ID
right JOIN 商品表 c ON a.商品ID=c.商品ID

declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename(仓库名称)+N'=sum(case when [仓库名称]=N'+quotename(仓库名称,'''')+N' then [商品数量] else 0 end)'
from #t group by 仓库名称
DECLARE @tbname VARCHAR(30)
SET @tbname='##t'+REPLACE(NEWID(),'-','')
exec(N'select [商品ID],商品名称'+@s+N',[合计]=isnull(sum(商品数量),0) into '+@tbname+' from #t group by [商品ID],商品名称 order by 商品ID')
EXEC ('select * from '+@tbname)



----------------结果----------------------------
/*
商品ID        商品名称 仓1          仓2          合计
----------- ---- ----------- ----------- -----------
101         花生   100         50          150
103         毛线   0           0           0
102         啤酒   80          0           80

*/


34,576

社区成员

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

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