更新时间:2023-09-23 13:58:22
您很亲密,但是要使此工作正常,您必须使用动态SQL构造PIVOT
,然后执行它.因此,在填充变量@Year之后,您需要执行以下操作:
You are close, but for this to work you have to construct your PIVOT
using dynamic SQL and then execute it. So, after you populate your variable @Year, you need to do something like this:
DECLARE @Query VARCHAR(MAX)
SET @Query = '
SELECT * from ( SELECT Amount, FYYear, column1, column2,column3 from BUYSCTE ) BUY
PIVOT( SUM(Amount) FOR FYYear in ('+ @Year + ') ) pvt'
EXEC(@Query)
尽管在执行此操作之前,您应该先查看此链接.
Though before doing this, you should take a look at this link.