gpt4 book ai didi

sql-server - 如何使用存储过程创建临时表

转载 作者:搜寻专家 更新时间:2023-10-30 22:31:36 30 4
gpt4 key购买 nike

我想在 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

我想在像这样的查询之后创建一个临时表

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怎么做到的

最佳答案

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

If Object_Id('Tempdb..#temp') Is Not Null
Drop Table #temp1
create table #temp(your columns)

Insert into #temp select...

或者使用 select into #temp like

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

关于sql-server - 如何使用存储过程创建临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687538/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com