开发者

Using declared variables as column names

I'm looking for a way to us开发者_JAVA百科e a dynamic variable as a column name as well - for example if I hypothetically use the following to define a financial year:

DECLARE @currentfy NVARCHAR(6) --Current financial year
SET @currentfy = YEAR(GETDATE()) - CASE WHEN MONTH(GETDATE()) < 4 THEN 1 ELSE 0 END

I then want to be able to do something like this:

SELECT @currentfy AS @currentfy
SELECT @currentfy - 1 AS @currentfy_1

So that it looks like as if I had done this:

SELECT 2010 AS [2010]
SELECT 2009 AS [2009]

Is there a way to do this without using dynamic pivoting? (as my tables are large and I want to avoid pivoting if possible).


No, use dynamic pivoting or an extra column/resultset to describe the subsequent columns/resultset

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜