如何使用存储过程创建TEMP表

时间:2017-03-09 05:35:06

标签: sql-server database

我想在SQL Server 2012中创建一个返回临时表的存储过程。

我的代码是

CREATE PROC [dbo].[aac_trial_balance_data]
    @company_code char(5),
    @target_level int,
    @StartDate char(12),
    @EndDate char(12)
AS
BEGIN
    SELECT
        dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
        level,
        SUM(debit) debit, 
        SUM(credit) credit
    FROM
        acc_trial_balance_vw 
    WHERE
        convert(datetime, create_date, 103) between convert(datetime, cast(@StartDate as datetime), 103) 
                                                and convert(datetime, cast(@EndDate as datetime) + '23:59:59', 103)  
        AND company_code = @company_code 
    GROUP BY 
        chart_code, LEVEL 
END

我想在查询之后创建一个Temp表,如

CREATE PROC [dbo].[aac_trial_balance_data]
    @company_code char(5),
    @target_level int,
    @StartDate char(12),
    @EndDate char(12)
AS
BEGIN
    (select 
            dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
            level,
            SUM(debit) debit, 
            SUM(credit) credit
        from acc_trial_balance_vw 
        where 
        convert(datetime,create_date,103) between convert(datetime, cast(@StartDate as datetime) , 103) 
        and  convert(datetime, cast(@EndDate as datetime)+'23:59:59' , 103)  
        and company_code = @company_code 
         GROUP BY chart_code, LEVEL 
         )
         AS
         #TEMP-TABLE -- This is my Temp Table That i want to create
END

id怎么做呢

5 个答案:

答案 0 :(得分:2)

你可以创建临时表,只需使用

select 
        dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
        level,
        SUM(debit) debit, 
        SUM(credit) credit into #tempTable
    from acc_trial_balance_vw 
    where 
    convert(datetime,create_date,103) between convert(datetime, cast(@StartDate as datetime) , 103) 
    and  convert(datetime, cast(@EndDate as datetime)+'23:59:59' , 103)  
    and company_code = @company_code 
     GROUP BY chart_code, LEVEL 

或使用select into #temp like

byte[]

答案 1 :(得分:0)

试试这个:

(select 
 dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
 level,
 SUM(debit) debit, 
 SUM(credit) credit

 INTO #THIS_TEMP_TABLE

 from acc_trial_balance_vw 
 where 
 convert(datetime,create_date,103) between convert(datetime, cast(@StartDate as datetime) , 103) 
 and  convert(datetime, cast(@EndDate as datetime)+'23:59:59' , 103)  
 and company_code = @company_code 
 GROUP BY chart_code, LEVEL)

 SELECT * FROM #THIS_TEMP_TABLE
 Drop table #THIS_TEMP_TABLE

答案 2 :(得分:0)

尝试在into #temptable dbname

之后插入from acc_trial_balance_vw
 CREATE PROC [dbo].[aac_trial_balance_data]
 @company_code char(5),
 @target_level int,
 @StartDate char(12),
 @EndDate char(12)
 AS
 BEGIN
    (select 
        dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
        level,
        SUM(debit) debit, 
        SUM(credit) credit
    into #TEMPTABLE -->>> Inserting here
    from acc_trial_balance_vw 

    where 
    convert(datetime,create_date,103) between convert(datetime, cast(@StartDate as datetime) , 103) 
    and  convert(datetime, cast(@EndDate as datetime)+'23:59:59' , 103)  
    and company_code = @company_code 
     GROUP BY chart_code, LEVEL 
     )
     AS

END

答案 3 :(得分:0)

create table #temp(company_code char(5),target_level int,StartDate char(12))
insert into #temp('','','')
select * from #temp

答案 4 :(得分:0)

select 
        dbo.getParentCode(chart_code,@target_level,LEVEL) chart_code,
        level,
        SUM(debit) debit, 
        SUM(credit) credit
        from acc_trial_balance_vw 
        where 
        convert(datetime,create_date,103) between convert(datetime, cast(@StartDate as datetime) , 103) 
        and  convert(datetime, cast(@EndDate as datetime)+'23:59:59' , 103)  
        and company_code = @company_code 
        GROUP BY chart_code, LEVEL 
相关问题