zoukankan      html  css  js  c++  java
  • 存储过程中使用临时表

    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
  • 相关阅读:
    CENTOS7下安装REDIS
    目录结构和文件路径
    bash特性
    (十六)MySQL集群galera实现
    (十五)mysql中间件MyCAT实现
    (4)三剑客之awk
    (十四)基于GTID的主从复制
    (十三)MySQL主从复制
    (3)三剑客之sed
    (十二)MySQL逻辑备份mysqldump
  • 原文地址:https://www.cnblogs.com/liujie2272/p/5505426.html
Copyright © 2011-2022 走看看