call a stored procedure sql server 2012 – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Tue, 03 Jan 2012 11:18:55 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 How to Call a stored procedure from other procedure https://sqlserverlearner.com/2012/01/03/how-to-call-a-stored-procedure-from-other-procedure/ https://sqlserverlearner.com/2012/01/03/how-to-call-a-stored-procedure-from-other-procedure/#respond Tue, 03 Jan 2012 11:18:55 +0000 https://sqlserverlearner.azurewebsites.net/2012/01/03/how-to-call-a-stored-procedure-from-other-procedure/ Following are the very basic questions on stored procedures.

Question: How can i call a stored procedure from another stored procedure?

Answer:
Use EXEC statement.
Following syntax to execute a stored procedure:

[sql]
EXEC STOREDPROCEDURENAME
[/sql]
Replace STOREDPROCEDURENAME with the name of the procedure.

You can add this piece of code in another stored procedure where ever required.

If the procedure has parameters, then below is the syntax.
[sql]
EXEC STOREDPROCEDURENAME @parameter1=value1,@parameter2=value2,..
[/sql]
Replace STOREDPROCEDURENAME with the name of the procedure, parameters with the name of parameters and values with values for the parameters.
If you have one procedure in schema1 which should call another procedure in schema2.

Following is the code that creates two stored procedures. One of the procedure calls the other procedure using EXEC.

[sql]
CREATE PROCEDURE Procedure1 –Creating Procedure1
AS
SELECT ‘1’
GO

CREATE PROCEDURE Procedure2 –Creating Procedure2 that calls Procedure1
AS
EXEC Procedure1 –CALLING PROCEDURE Procedure1
GO

EXEC Procedure2 –Executing Procedure2
[/sql]

Question: How can i call a stored procedure from another stored procedure, both of them are from different schemas?

Answer:
USE EXEC Schemaname.procedurename

[sql]
EXEC SchemaName.ProcedureName
[/sql]

Following is the simulation and solution for this question.

[sql]
CREATE SCHEMA Schema1 –Creating Schema1

GO

CREATE SCHEMA Schema2 –Creating Schema2
GO

CREATE PROCEDURE Schema1.Procedure1 –Creating Procedure1 in Schema1
AS
SELECT ‘1’
GO

–Creating Procedure2 in Schema2 to call Procedure1 in Schema1
CREATE PROCEDURE Schema2.Procedure2
AS
EXEC Schema1.Procedure1 –CALLING SCHEMA1 PROCEDURE FROM SCHEMA2 PROCEDURE
GO

–Executing Procedure2 in Schema2
EXEC Schema2.Procedure2
[/sql]

Now if you execute:

[sql]
EXEC Schema2.Procedure2
[/sql]

You can see that it is calling Schema1.Procedure1 and displaying 1 as the result.

]]>
https://sqlserverlearner.com/2012/01/03/how-to-call-a-stored-procedure-from-other-procedure/feed/ 0