Click here to Skip to main content
15,890,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

Here i want to Know there is any possible way to create StoredProcedure

Inside another StoredProcedure.

Means:I want to use StoredProcedure as MasterClass.So we can Reduce the Memory.Execution will be fast.I think so.if is there any Option please give me the Suggestions.


Regards,

Anilkumar.D
Posted

Procedures are not classes so you cannot nest them directly. They are more like methods of a class.

You can call another procedure from inside of a procedure. And what comes to speed, one idea in the procedure is that the procedure is precompiled in the memory. This way the execution is (quite) fast even with new parameters as long as the procedure is well defined.
 
Share this answer
 
Comments
Anil Honey 206 12-Aug-11 3:31am    
Thanks For ur Suggestions.Even Sqlserver is Developed through OOPS Concepts.So We can declare Procedure as Class is it Right na.So the Execution Come faster.Example like Polymorphism.So can u give me one Example which i posted.So i can Understand Clearly.

Regards,

Anilkumar.D
Wendelius 12-Aug-11 4:32am    
As said, stored procedures don't behave like classes so you cannot for example nest them like in C#:
class A {
class B {
...
}
}


Also as said you can call a stored procedure from within a stored procedure, basically like calling a method from a method.

One definition for a procedure: "A stored procedure is a group of Transact-SQL statements compiled into a single execution plan." T-SQL is a procedural language not object oriented.
Anil Honey 206 12-Aug-11 4:50am    
As per u Sql is Procedure Language.we cant Implements the Methods of Opps.
Wendelius 12-Aug-11 13:50pm    
Transact-SQL (T-SQL) is procedural so it cannot be used in an object oriented way. Certain features in SQL Server support object oriented approach better, but not procedures.
Yes. You can use a stored procedure inside another stored procedure.
Check this link.

http://www.aspfree.com/c/a/ASP.NET-Code/Call-Stored-procedure-from-within-another-stored-procedure-return-values/[^]

OP: plz Dont think wrong i cant Rediret to ur link some secutity issues.So can u send me an Example.

This quick how-to shows an example of having two stored procedures that one stored procedure executes the other stored procedure passing values and returning a value to the calling parent stored proc. This would be handy passing in a value to dynamically populate a lookup table and stored that Id as a foreign key in a datatable. . This example asssumes you would have a parent table and a child/lookup table related back to the parent table.
SQL
--This stored procedure is called by a business component or webpage 
--passing into two parameters, executes sp_ChildStoredProc and passes on value. 
--The @Id OUTPUT returns the value.  

CREATE PROCEDURE sp_ParentStoredProc
(
@col1 varchar(20)
@ValuePassed varchar(50),
)
AS

declare @Id int
EXEC sp_ChildStoredProc @ValuePassed, @Id OUTPUT

INSERT INTO SomeTable
(col1, col2)
VALUES
(@col1, @Id)
RETURN
GO 

This stored procedure is executed in the sp_ParentStoredProc above and passes'a varchar variable piece of data. The spChildStoredProc will insert this piece of data'into a lookup table if it doesn't exist, return the Identity column using Scope_Identity() function or'will return the identity value to the parent stored proc if the piece of data passed in already exists in the lookup table.
SQL
CREATE PROCEDURE sp_ChildStoredProc

(
@ValuePassedIn varchar(50),
@Id smallint OUTPUT
)
 AS

If exists (SELECT column1 FROM LookUpTable WHERE column1=@ValuePassedIn)
BEGIN
SET @Id = (SELECT column1 
   FROM LookUpTable 
   WHERE column1=@ValuePassedIn)
END 
ELSE
BEGIN
INSERT INTO LookUpTable(column1) 
VALUES(@ValuePassedIn)
    
    SET @Id = Scope_Identity()
END
GO
 
Share this answer
 
v3
Comments
Anil Honey 206 12-Aug-11 3:19am    
plz Dont think wrong i cant Rediret to ur link some secutity issues.So can u send me an Example.
Toniyo Jackson 12-Aug-11 3:27am    
Check the updated answer!
Anil Honey 206 12-Aug-11 4:36am    
Thanks for giving the idea of the Concept.
OriginalGriff 12-Aug-11 3:41am    
He gave me the "security issues" line as well - I suspect that he doesn't want to read, but just get given his solution (which judging by his other responses is not going to be what he thinks it is...)
Anil Honey 206 12-Aug-11 4:34am    
Sorry what u think about me is wrong.In my Company Google is not allowed.And even iam not judgeing others Knowledge.While iam writting some stored Procedure.I got an idea is there any possible way we can create storedprocedure with another storedprocedure.So i posted my thought.if ur thinking like that sorry.
Yes: you can create Stroed Procedures within other stored procedures, and execute them as well. There is some info here: How to Share Data Between Stored Procedures[^]

Be aware that is may not speed anything up to create procedures inside other procedures - there will be a creation overhead within the server which you may not have accounted for!
 
Share this answer
 
Comments
Anil Honey 206 12-Aug-11 3:25am    
Thanks For your Valuable Suggestion.I want one more Help from u.Atpresent i cant redirect to ur link.Due to some security reasons.So plz can u send one example.
Regards,

Anilkumar.D
You can nest[^] stored procedure calls - that is, call one from another upto 32 levels.

However, there is no need to be able to create one stored procedure inside another so that requirement does not make much sense.
 
Share this answer
 
Comments
Anil Honey 206 12-Aug-11 3:35am    
So it not make such sense.May be sharing some Good Knowledge.we are using Polymorphism.Like that iam expected.

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