码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程中使用临时表

时间:2016-05-18 16:07:07      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

CREATE PROCEDURE [dbo].[usp_UserFriendsOrder]  
 @s varchar(MAX)  
as
--如果临时表存在则先删除
if exists(select * from dbo.sysobjects where object_id(tempdb.dbo.#temp) is not null)
 delete from #temp
else
  create table #temp(F_ID int,F_UserID char(36),F_FatherID int,F_Code varchar(18),F_UserName varchar(20),F_Phone varchar(11),F_CreateUser char(36),F_CreateDate datetime, F_State int,F_Uplevel varchar(8000),F_DownLevel varchar(8000),F_Level int)
  --执行存储过程usp_TUserRelationByFID 放入临时表
  insert into #temp exec usp_TUserRelationByFID @s
  --查询临时表
   -- select * from #temp 
BEGIN
 -- A.Currentcount 本月消费条数
 -- B.Lastcount    上月消费条数
declare  @sqlf_id varchar(MAX) 
set @sqlf_id=SELECT  ltrim(Convert(numeric(9,2),case when B.Lastcount=0 then 0 else A.Currentcount*100.0/B.Lastcount end))+‘‘%‘‘ As Percentage, A.Currentcount,B.Lastcount  FROM (SELECT      count(*) as Currentcount
FROM         #temp  INNER JOIN 
                      dbo.T_Order ON #temp.F_UserID = dbo.T_Order.F_CreateUser  where  dbo.T_Order.F_Remark!=‘‘商家收银‘‘ and  datediff(month,dbo.T_Order.f_createdate,getdate())=0)as A,(SELECT      count(*) as Lastcount
FROM         #temp  INNER JOIN 
                      dbo.T_Order ON #temp.F_UserID = dbo.T_Order.F_CreateUser  where  dbo.T_Order.F_Remark!=‘‘商家收银‘‘ and  datediff(month,dbo.T_Order.f_createdate,getdate())=1)as B
exec (@sqlf_id)
end

 

存储过程中使用临时表

标签:

原文地址:http://www.cnblogs.com/liujie2272/p/5505426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!