If I have a master stored procedure that runs other stored procedures, do those stored procedures complete execution in the order they were listed?
Ex: p_master
exec p_sp1
exec p_sp2
exec p_sp3
Will p_sp1 complete execution before p_sp2 executes, and will p_sp2 complete execution before p_sp3 executes?
If not, is it possible to force this to behave this way?
Short answer: Yes
As an aside, if you execute the stored procedures individually (i.e. not inside a master stored procedure), you should execute them like this:
exec p_sp1
Go
exec p_sp2
Go
exec p_sp3
Go
Reference
Related
I have the following stored procedure in Sybase 16,
create or replace procedure ... as
...
drop table tempdb..koppelingen
go
declare
vre_cursor cursor for
...
Then I see: declare cursor must be the only statement in a query batch.
If I skip the go, I can create the stored procedure.
If I execute the code of the stored procedure by hand by selecting it and execute, I have to use the go.
So what happens in a stored procedure? Does it insert the go's by itself? But then I do not understand the error message of declare cursor above.
go is not an ASE command.
go is a client-side command that tells the client application (eg, isql) that a batch of SQL can now be sent to ASE. In the case of the create or replace procedure ... the go tells the client application (eg, isql) that you've completed the stored proc definition and it can now be submitted to ASE for parsing & compiling.
declare cursor must be in a batch of SQL by itself (ie, declare ...\ngo) if being run from a client application (eg, isql); when inside a stored proc the declare cursor command can be grouped with other follow-on commands (eg, open, fetch).
Sometimes it happens that I have some errors in stored procedure body, e.g.
select invalidColumn from validTable
When I try to create/alter stored procedure, it works and I can see the error only when I try to execute the stored procedure.
Is it possible to force schema check when I create or alter?
I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?
If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.
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
I need to create a stored procedure one time and in one place only on my database server, but be able to run it from any database. However, I don't want to have to type database.schema.procedure_name every time I run it. I'd like to be able to use it like the built in procedures: sp_... is there any way to do this?
here is the procedure that I'm trying to create:
--actual procedure is more complex
CREATE PROCEDURE TestProcedure
AS
select * from sys.dm_exec_requests
go
here is how I'd like to run it from SQL Server Management Studio:
EXEC TestProcedure
--or
TestProcedure
however, you you get this error:
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'TestProcedure'.
everything runs fine if I run it with:
EXEC master.dbo.TestProcedure
--or
master.dbo.TestProcedure
you can run system procedures like this without any errors:
EXEC sp_help
--or
sp_help
Just create it in the master database and give it the sp_ prefix and it will work. SQL Server always checks the master database first for stored procedures (and other types of objects as well) with this prefix.
It will handle the datbase context for you as well as below.
USE master
create proc [dbo].[sp_test] as
select db_name()
GO
USE YourDB
EXEC master..sp_test /*Returns "master"*/
EXEC sp_test /*Returns "YourDB"*/
Edit:
Just following up Cade's comment to mark it as a system procedure. This seems like it might well be required in some circumstances. In SQL 2005+ you would use
EXEC sys.sp_MS_marksystemobject 'dbo.sp_test'