我有一个任务要向查询发送一个成本中心列表(varchar),该查询应根据每个输入提供数据。
我对动态SQL不了解。
所以我有一个成本中心列表,例如88115100
,88115200
,88115300
......大约30。
declare @month integer
declare @month_2 integer
declare @startdate datetime
declare @enddate datetime
declare @costcenter varchar(20)
set @month_2= 3--/* t3.finncPriod=*/ '[%0]'
set @month=@month_2-1
set @costcenter='88115100'
set @startdate= '03/01/2019'--/*t0.docdate from oinm t0 where t0.docdate>=*/'[%1]'
set @enddate= '03/31/2019'--/*t0.docdate from oinm t0 where t0.docdate>=*/'[%2]'
select * from
(
select
t0.name+'-Budget' as 'toPivot',
t4.AcctCode as 'GLAccount',
t4.AcctName,
isnull(t2.DebLTotal,0) as 'value'
from obgs as t0
inner join obgt as t1 on t0.AbsId = t1.Instance
inner join bgt1 as t2
on t2.Instance = t1.Instance
and t2.AcctCode = t1.AcctCode
and t2.Line_ID = @month
inner join oact as t4 on t4.AcctCode = t2.AcctCode
where t0.name = @costcenter
union all
select
t0.name + '-Actual'as 't0Pivot',
t4.AcctCode as 'GLAccount',
t4.AcctName,
isnull(sum(debit-credit),0) as 'value'
from jdt1 as t3
left join oact t4 on t4.AcctCode = t3.Account
left join obgs t0 on t0.Name = t3.ProfitCode
left join obgt t1
on t1.Instance = t0.AbsId
and t4.AcctCode = t1.AcctCode
left join bgt1 as t2
on t1.Instance = t2.Instance
and t2.AcctCode = t1.AcctCode
and t2.line_ID = 2
where
t3.refdate between @startdate and @enddate
and t4.ExportCode is not null
and t0.name = @costcenter
group by
t0.name,
t4.AcctCode,
t4.AcctName
) as a
pivot
(
sum(a.value)
for a.topivot in
([88115100-Budget], [88115100-Actual])
)
as pivottable
答案 0 :(得分:0)
好吧,据我所知,您可能想要stored procedure,并且您建议它可以/应该是动态SQL。现在,您可以做到这一点,但是唯一需要动态SQL的地方是必须根据变量重写关键字。您必须使用上面的代码来执行此操作-特别是以下代码:
pivot
(
sum(a.value)
for a.topivot in
([88115100-Budget], [88115100-Actual])
)
这是因为[88115100-Budget]
和[88115100-Actual]
是取决于变量(@costcenter
)的关键字。但是,没有理由让它们在SQL中这样命名!最好以后再使用这些名称,无论将数据发送到SQL外部是什么。
相反,您可能需要类似以下代码的内容:
CREATE PROCEDURE GetBudgetForCostCenter
@month integer = 2,
@month_2 integer = 3,
@startdate datetime = '03/01/2019',
@enddate datetime = '03/31/2019',
@costcenter varchar(20) = '88115100'
--you probably don't actually want the default values in here, but I've left them for demonstration.
AS
BEGIN
select * from
(
select
'Budget' as 'toPivot',
t4.AcctCode as 'GLAccount',
t4.AcctName,
isnull(t2.DebLTotal,0) as 'value'
from obgs as t0
inner join obgt as t1 on t0.AbsId = t1.Instance
inner join bgt1 as t2
on t2.Instance = t1.Instance
and t2.AcctCode = t1.AcctCode
and t2.Line_ID = @month
inner join oact as t4 on t4.AcctCode = t2.AcctCode
where t0.name = @costcenter
union all
select
'Actual'as 't0Pivot',
t4.AcctCode as 'GLAccount',
t4.AcctName,
isnull(sum(debit-credit),0) as 'value'
from jdt1 as t3
left join oact t4 on t4.AcctCode = t3.Account
left join obgs t0 on t0.Name = t3.ProfitCode
left join obgt t1
on t1.Instance = t0.AbsId
and t4.AcctCode = t1.AcctCode
left join bgt1 as t2
on t1.Instance = t2.Instance
and t2.AcctCode = t1.AcctCode
and t2.line_ID = 2
where
t3.refdate between @startdate and @enddate
and t4.ExportCode is not null
and t0.name = @costcenter
group by
t0.name,
t4.AcctCode,
t4.AcctName
) as a
pivot
(
sum(a.value)
for a.topivot in
([Budget], [Actual])
)
as pivottable
END
请注意,我已将您的列从88115100-Budget
和88115100-Actual
更改为Budget
和Actual
。这意味着您可以避免使用动态SQL,这是您应该始终要实现的目标。
现在应该可以通过类似的方式调用
exec GetBudgetForCostCenter @month = 2, @month_2 = 3, @startdate = '03/01/2019', @enddate = '03/31/2019', @costcenter = '88115100'
免责声明:我不会比使用内置语法突出显示测试语法更多的麻烦,因为它将需要创建一个新的数据库和许多新的对象,因此,如果其中有任何错别字,就对不起。