I'm new at writing these stored procedures, so bare with me... I am trying have one stored procedure call upon another, and am confused. I've read that you can have up to 32 stored proceedures within each other ect... But, I guess I'm doing it wrong. I have the following 2 SP's and how would I get #2 to call upon the first SP's results in order to get a count of the records within a specific age for #2. I have several other SQL selects just like #2 that I need to addon to this so that I can obtain a count of the various items based upon their ages...?
-----------------------------------------------
#1
CREATE PROCEDURE [dbo].[GetMyData_sp]
@ReportDate char (10),
@Market char (50),
@RepCenter char (50),
@RepTDL char (10),
@RepLname char (30)
AS
DECLARE @SQL varchar (1000)
SELECT @SQL = 'SELECT Exceptions, Status, ReportDate, ExceptionDate ' +
' Market, RepCenter, RepTDL, RepLname, Age ' +
' FROM [dbo].data ' +
' WHERE (ReportDate = ''' + @ReportDate + ''') '
IF @Market <> NULL
BEGIN
SELECT @SQL = @SQL + ' AND (Market = ''' + @Market + ''') '
END
IF @RepCenter <> NULL
BEGIN
SELECT @SQL + @SQL + 'AND (RepCenter = ''' + @RepCenter + ''') '
END
IF @RepTDL <> NULL
BEGIN
SELECT @SQL = @SQL + 'AND (RepTDL = ''' + @RepTDL + ''') '
END
IF @RepLname <> NULL
BEGIN
SELECT @SQL = @SQL + 'AND (RepLname = ''' + @RepLname + ''') '
END
SELECT @SQL = @SQL + 'GROUP BY RepCenter, RepTDL, RepLname, ReportDate, Market, Exceptions, ExceptionDate, Status, AGE '
EXEC (@SQL)
GO
-------------------------------------------------------
#2
CREATE PROCEDURE [dbo].[GetAGE1_sp]
AS
SELECT Exceptions, Status, Count(Status) AS AGE1
FROM GetMyData_sp
WHERE (Age Between 1 and 2)
GROUP BY Exceptions, Status
GO
-------------------------------------------------------
-----------------------------------------------
#1
CREATE PROCEDURE [dbo].[GetMyData_sp]
@ReportDate char (10),
@Market char (50),
@RepCenter char (50),
@RepTDL char (10),
@RepLname char (30)
AS
DECLARE @SQL varchar (1000)
SELECT @SQL = 'SELECT Exceptions, Status, ReportDate, ExceptionDate ' +
' Market, RepCenter, RepTDL, RepLname, Age ' +
' FROM [dbo].data ' +
' WHERE (ReportDate = ''' + @ReportDate + ''') '
IF @Market <> NULL
BEGIN
SELECT @SQL = @SQL + ' AND (Market = ''' + @Market + ''') '
END
IF @RepCenter <> NULL
BEGIN
SELECT @SQL + @SQL + 'AND (RepCenter = ''' + @RepCenter + ''') '
END
IF @RepTDL <> NULL
BEGIN
SELECT @SQL = @SQL + 'AND (RepTDL = ''' + @RepTDL + ''') '
END
IF @RepLname <> NULL
BEGIN
SELECT @SQL = @SQL + 'AND (RepLname = ''' + @RepLname + ''') '
END
SELECT @SQL = @SQL + 'GROUP BY RepCenter, RepTDL, RepLname, ReportDate, Market, Exceptions, ExceptionDate, Status, AGE '
EXEC (@SQL)
GO
-------------------------------------------------------
#2
CREATE PROCEDURE [dbo].[GetAGE1_sp]
AS
SELECT Exceptions, Status, Count(Status) AS AGE1
FROM GetMyData_sp
WHERE (Age Between 1 and 2)
GROUP BY Exceptions, Status
GO
-------------------------------------------------------