Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir/mam,
If we create a stored procedure or function in SQL SERVER and it returns a table and again if we wish to create another stored procedure or function that will make a call to the previous procedure or function between the code, how shall we handle the return type that is returned by first procedure or function. sir, I mean to say 1st procedure or function returns a table that should be hold in a "table" type varriable in the same way as integer is hold in "integer" type variable. But i am unable to get that type of variable. So what should i do to hold the table in a varriable ?
Posted

Create your first Stored Procedure which returns a table
The code for the stored procedure might look like this-

SQL
CREATE PROCEDURE dbo.GetDetailByFname
    (
    @Fname nvarchar(50)
    )
AS
    Select pid, Fname, Mname, Lname from Personal where fname=@Fname;


If you call this stored Procedure as
SQL
EXEC dbo.GetPeopleByLastName @FName = 'Rahul'

Result will look like
pid                FName    MName      LNAme
uniquidentifier    Rahul     .          Singh
uniquidentifier    Rahul     abc        Bachchan
--------           Rahul     XYZ        Rathod


If you want to use this result you'll need a place to store it, that is Temporary Tables . That code might look something like this-

SQL
CREATE PROCEDURE dbo.CountByLname
As
DECLARE  @Personal TABLE (
    Pid uniqueidentifier,
    FName NVARCHAR(50),
    Mname NVARCHAR(50),
    LName NVARCHAR(50)
)

INSERT @Personal (Pid, FName, Mname, LName)
EXEC dbo.GetDetailByFname @Fname = 'Rahul'

SELECT COUNT(*) AS Counts FROM @Personal

If you call this stored Procedure as
SQL
EXEC CountByLname

Result will look like

Counts
3

This is how we can call 1st procedure or function that returns a table in 2nd stored procedure or function
If you want Integer as return type
Try this one as 1st stored procedure
CREATE PROCEDURE dbo.GetDetailByFname
	(
	@Fname nvarchar(50),
              @Age INT  OUTPUT
	)
AS
Select Age from Personal where Fname=’Rahul’

Then use this output variable in 2nd stored procedure

I hope it's helped you..

Regards,
jayant
 
Share this answer
 
v3
You can create a function that returns a table then use it. Something like this:
Create Function SomeFunction
returns table as

return (select * from someTable)


in the stored procedure, you can have something like this:

select * from SomeFunction


Here, you can make use to into and temporary tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900