存儲過程中使用臨時表


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM