Reputation: 681
I have a stored procedure with takes 2 parameters (StudentID and CurrentSmester) as input and return me table with fields :
This stored procedure is created by another team in my office. I can not modify it but at the same point I want to use it in my Webservice (which I am building for android) to make sure data remains consistent.
My requirement is to get :
Can I create another store procedure which will call that store procedure with parameters as I mentioned, make a join with course table to get course title too.
Is this possible ? If yes can you please guide me through its implementation.
Thanking You and Happy New Year !!
Upvotes: 1
Views: 769
Reputation: 666
-- First Stored Procedure
CREATE PROCEDURE FirstSP
@MyFirstParam INT
AS
DECLARE @MyFirstParamValue INT
SELECT @MyFirstParamValue = @MyFirstParam * @MyFirstParam
RETURN (@MyFirstParamValue)
GO
-- Second Stored Procedure
CREATE PROCEDURE SecondSP
@SecondParam INT
AS
DECLARE @SecondParamValue INT
SELECT @SecondParamValue = @SecondParam * @SecondParam
RETURN (@SecondParamValue)
GO
-- Pass One Stored Procedure's Result as Another Stored Procedure's Parameter
DECLARE @FirstValue INT, @SeondValue INT
-- First SP
EXEC @FirstValue = FirstSP 5
-- Second SP
EXEC @SeondValue = SecondSP @FirstValue
SELECT @SeondValue SecondSP
GO
-- Clean up
DROP PROCEDURE FirstSP
DROP PROCEDURE SecondSP
GO
Upvotes: 0
Reputation: 69514
Create a new stored procedure , insert the results coming back from your existing stored procedure into a temp table, join your Course table with that temp table and you are good to go ,
something like this.....
CREATE PROCEDURE usp_NewProc
@StudentID INT ,
@CurrentSmester INT
AS
BEGIN
SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#temp', 'U') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp
(
CourseCode [DataType],
UpdateType [DataType],
Update Id [DataType]
)
INSERT INTO #temp
EXEC existsting_proc @StudentID , @CurrentSmester
SELECT t.* , C.CourseTitle
FROM #temp t INNER JOIN CourseTable C on <join Condition>
IF OBJECT_ID('tempdb..#temp', 'U') IS NOT NULL
DROP TABLE #temp
END
Upvotes: 1
Reputation: 166376
You can insert the results from the STORED PROCEDURE into a temp table (table variable or temp table) and them select from that table and join onto the Courses
table to retrieve the title.
Example code
CREATE TABLE Courses(
CourseCode VARCHAR(50),
CourseName VARCHAR(250)
);
INSERT INTO Courses VALUES ('A','AA'), ('B','BB');
CREATE PROCEDURE OtherTeamsSP(
@StudentID INT,
@CurrentSmester INT
)
AS
SELECT 'A' CourseCode,
'FOO' UpdateType,
1 UpdateId;
CREATE PROCEDURE MyProcedure(
@StudentID INT,
@CurrentSmester INT
)
AS
CREATE TABLE #SPOutput(
CourseCode VARCHAR(50),
UpdateType VARCHAR(50),
UpdateId INT
)
INSERT INTO #SPOutput
EXEC OtherTeamsSP @StudentID, @CurrentSmester
SELECT *
FROM #SPOutput s INNER JOIN
Courses c ON s.CourseCode = c.CourseCode
DROP TABLE #SPOutput
Calling the new SP
EXEC MyProcedure 1,2
Upvotes: 0