SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Analysis Server and Reporting Services (2008)
 Dynamic column name depend on date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

micnie_2020
Posting Yak Master

Malaysia
225 Posts

Posted - 01/15/2014 :  17:19:21  Show Profile  Reply with Quote
Hi,

I have SP as below, my column name is depend on date input. How can I create SSRS report dataset since I have no static column name?

Refer 2 example table column name with 2 different input:
http://oi44.tinypic.com/141a87.jpg



Create procedure CALENDARFY_Display (@START_DATE DATETIME,@ENDDATE DATETIME) AS
--exec CALENDARFY_Display '2011-04-01', '2012-03-31'
--DECLARE @START_DATE DATETIME
--DECLARE @ENDDATE DATETIME
--SET @START_DATE = '2011-04-01'
--SET @ENDDATE = '20120331';

delete from CALENDAR
BEGIN
WITH CTE_DATES AS
(
SELECT
    @START_DATE DateValue UNION ALL SELECT
    DateValue + 1
FROM CTE_DATES
WHERE DateValue + 1 <= @ENDDATE
)
INSERT INTO CALENDAR (Dates)
    SELECT
        CAST(DateValue AS date)
    FROM CTE_DATES
    OPTION (MAXRECURSION 0)
END
    
drop table tempp

 select 
 Dates=replace(convert(varchar(10), c.Dates, 120),'-','')+'|'+datename(weekday, c.Dates ),
 e.UserName,Descs
into tempp
 from Calendar c
left join 
(
SELECT id,Dt,u.UserName,Descs=cast(isnull([text],'') as varchar)+'-'+cast(Isnull(room_id,'') as varchar) 
from events e
left join aspnet_Users u on u.[userId]=e.[user_id] 
CROSS APPLY fnGetDatesInRange([Start_Date], End_Date)
)  e on convert(varchar(10), Dt, 120)=c.Dates
where dates between @START_DATE and @ENDDATE











DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Dates) 
                    from tempp
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT UserName ,' + @cols + '
            from 
            (
                select Dates, UserName, Descs
                from tempp
            ) x
            pivot 
            (
                max(Descs)
                for Dates in (' + @cols + ')
            ) p where UserName is not null'

execute(@query)





Please advise.

Regards,
Micheale

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 01/16/2014 :  05:23:49  Show Profile  Reply with Quote
just name columns sequentially as 1,2 ,3... always instead of based on arriving date values. this will make sure the metadat remains consistent and you'll be able to use it in SSRS dataset.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000