Wednesday, March 28, 2012

how to create parameterised views?

I am using SqlServer2005 and asp.net 2005. I have a large database for which I have to provide reports for it. I need a report that user will specify a date, and the report will be run, from this month & year and I will calculate the first day of the month and the last day of the month and these will serve as the input parameters for a long SELECT query.

For my previous reports i've used views . But for this functionality i need a parameterized view, which i cant create.

Please help me how to create a parameterized view...

Thanks in Advance...

Use a stored procedure.

|||

Given a date and if you need to find the first day of the month/last day of the month here's how you can do it.

DECLARE @.DdatetimeSET @.D =getdate()SELECT ,DATEADD(dd, -DATEPART(dd,GETDATE()) + 1,GETDATE())'First day of the month' ,DATEADD(d, -DAY(DATEADD(m,1,@.Date)),DATEADD(m,1,@.Date))AS'Last day of the month'

You can use the same in a view as follows:

SELECTFROM dbo.vwSomeViewWHERE DatecolumnBETWEEN @.begDateAND @.EndDate
|||

You can't create a parameterized view. You can create a view and then filter it. Or you can create a table valued function, which is pretty close to a parameterized view.

No comments:

Post a Comment