Does anyone know of a way to execute a stored procedure from within a stored procudure using some of the params passed in from the original procedure.
i.e.
if i pass in params #id, #event date, # log into sproc 1
then sproc 1 will do it's own stuff and pass in #log and #event_date to sproc 2.
Thanks!
just call:
exec mysproc #param1 #param2
inside your sproc.
Just execute the second sp the same way you would have from a query.
EXEC SPROC2 #LOG, #EVENT_DATE
call exec with the parameters of interest:
... within sp_1
... other code
exec sp_2 #log, #event_date
Related
I want to do something like this:
CREATE OR ALTER PROC myPROC
#myInput NVARCHAR(MAX)
AS
BEGIN
SELECT *
FROM myTABLE
-- I want this next command to run not to be stored for later execution.
EXEC RegisterProcSomewhere
END
GO
Is there a preferred way to do this? Essentially I want to collect metadata on my procedure when I CREATE/ALTER them.
Call it outside the proc to execute it immediately.
CREATE OR ALTER PROC myPROC
#myInput NVARCHAR(MAX)
AS
BEGIN
SELECT * FROM myTABLE
-------------------------------
END
GO
--I want this next command to run not to be stored for later execution.
EXEC RegisterProcSomewhere
I want to loop through a bunch of different functions and execute them inside a stored procedure. For example, if I have the function names:
Function1
Function2
Function3
And I want to call each using the variable name, #fn, how do I do that? I already know how to store the functions inside #fn and loop using a cursor, but do not know how to execute it. The following is what I've tried:
declare #fn nvarchar(30) = 'Function1',
#sql varchar(500);
set #sql = N'select * from dbo.' + #fn + '(101)';
exec #sql;
I've also tried exec sp_executesql #sql;
Thanks!
To execute dynamic SQL with EXEC you have to wrap EXEC's argument into parenthesis or it would think you are trying to execute stored procedure:
exec (#sql);
You can write a dynamic query and pass that dynamic query in a paramater and call in EXEC #parameter
Follow the link it will help
EXECUTE (Transact-SQL)
call another sp with output inside stored procedures sql server
Initially I had one stored procedure and everything was running good but the requirement has changed now and I would like to call multiple stored procedures and pass a parameter. I am using Execute SQL Task and here is my query where it calls only one stored procedure:
EXEC [dbo].[sp_TEST]
#Code =?
[PEDS].[up_InitialClear]
The above works well but I need to be able to call other stored procedure and pass the same parameter here. How can I call sp_TEST1, sp_TEST2 and pass the same parameter? Thanks
Simply execute them separately like below .
EXEC [dbo].[sp_TEST] #Code
EXEC [PEDS].[up_InitialClear] #Code
How one can make an async call to a stored procedure from another one?
Assume I have two stored procedures, SP1 and SP2 (this is a long running stored procedure, takes much time to execute, and doesn't return any result).
The stored procedure SP1 is defined like this:
CREATE PROCEDURE SP1
AS
BEGIN
--custom business logic
--CALL to SP2, but async
EXEC SP2
END
How could you make a non-blocking/async call to SP like the above in SQL Server 2008/2012?
There was once I tried to achieve this by wrapping the stored procedure into Job, and then Calling the job in the procedure through sp_start_job system sp.
EXEC dbo.sp_start_job N'Job name' ;
Blockquote
Works as long as there are no arguments. – RoastBeast Dec 22 '15 at 17:30
Here's version with passing parameters
declare #variable -- job name
declare #command -- command
set #command = 'select * from table where data='+#variable
exec msdb..sp_add_job
#job_name =#variable,
#enabled=1,
#start_step_id=1,
#delete_level=1 --Job will delete itself after success
exec msdb..sp_add_jobstep
#job_name=#variable,
#step_id=1,
#step_name='step1',
#command=#command
exec msdb..sp_add_jobserver
#job_name = #variable,
#server_name = 'yourserver'
exec msdb..sp_start_job
#job_name=#variable
I have created a stored procedure in SQL Server 2005, also I create another stored proc (that accepts some input parameters), I have to invoke with the use of the first proc.
CREATE PROCEDURE prc_inner (#value INT)
AS
SELECT #value
GO
CREATE PROCEDURE prc_outer (#value INT)
AS
EXEC prc_inner #value
GO
EXEC prc_outer 1
Look up EXEC sp_executesql or just plain EXEC [storedprocedurename]