I have a stored procedure usp_gethomedata(<current year>)
that takes only current year (2012) as parameter and displays data for 01-01-2012
to current date of 2012 ie 26-11-2012.
I have another stored procedure that shows similar data in usp_gethomedata(<start date>, <end date>)
that takes 2 parameter start and end date ranges from 01-01-1900
to 26-11-2012
.
Now, I am not sure how to call the second stored procedure from inside the 1st one so that it display me data for 01-01-2012
to 26-11-2012
provided I have to keep in mind.. that while calling the 1st procedure I can supply only the 2012 ie the current year as a parameter from my asp .net application.
Please help.
No you cannot create overloaded stored procedure. One will override the other. Though Overloaded function is possible. So, change the inner stored procedure to some different name or the vice versa (ie outer stored procedure). Here is an example that will simulate your situation.
-- Outer stored procedure
USE [Test]
GO
-- exec [dbo].[USP_GetHomeData] 2012
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_GetHomeData]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[USP_GetHomeData]
GO
CREATE PROCEDURE [dbo].[USP_GetHomeData]
-- Add the parameters for the stored procedure here
(
@Year INT
)
AS
BEGIN
DECLARE @startDate DATETIME='1/1/' + CAST(@Year AS VARCHAR(4)) -- mm/dd/yyyy
DECLARE @endDate DATETIME=GETDATE() -- mm/dd/yyyy
SELECT [Date] = DATEADD(Day,Number,@startDate)
FROM master..spt_values WITH(NOLOCK)
WHERE Type='P'
AND DATEADD(day,Number,@startDate) <= @endDate
exec [dbo].[USP_GetHomeData_Inner] '1/1/1900', @endDate
END
-- Inner stored procedure
USE [Test]
GO
-- exec [dbo].[USP_GetHomeData_Inner] '1/1/1900', '11/25/2012'
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USP_GetHomeData_Inner]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[USP_GetHomeData_Inner]
GO
CREATE PROCEDURE [dbo].[USP_GetHomeData_Inner]
-- Add the parameters for the stored procedure here
(
@startDate DATETIME
,@endDate DATETIME
)
AS
BEGIN
;WITH Calender AS
(
SELECT @startDate AS CalanderDate
UNION ALL
SELECT CalanderDate + 1 FROM Calender
WHERE CalanderDate + 1 <= @endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
FROM Calender WITH(NOLOCK)
OPTION (MAXRECURSION 0)
END
The result (partial) is as under
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.