Declaring stored procedures header and body separately - sql

I'm writing stored procedures in SQL Server 2008. I need to create two procedures which will use each other.
Procedure A executes B and B executes A.
So I suppose I have to declare headers of procedures firstly. I've searched over internet but I can't find the answer how can I make it.
So, my question is: how can I declare header and body of stored procedure separately in SQL Server 2008?

You can't separate them but you can create a SP that calls another SP that not yet exist.
When you run this
create procedure TheFirst as
begin
exec TheSecond
end
and SP TheSecond does not exist you will get a message:
The module 'TheFirst' depends on the missing object 'TheSecond'. The
module will still be created; however, it cannot run successfully
until the object exists.
Update:
To avoid the messages you can first create an empty proc and add the code later in the script.
create procedure ProcB as
go
create procedure ProcA as
begin
exec ProcB
end
go
alter procedure ProcB as
begin
exec ProcA
end

Related

Can I have the same stored procedure with two different name?

Bellow, I have a stored procedure named usp_customer
CREATE PROCEDURE usp_customer
AS
BEGIN
SELECT * FROM CUSTOMER
END
I want to give the same exact stored procedure a second name usp_cust1
Note: I am not looking to rename or to create a new stored procedure, I want both names to work
In the end, I could use either EXEC usp_customer or EXEC usp_cust1
Thanks
edit: changed sp_ to usp_
One stored procedure can call another:
CREATE PROCEDURE usp_cust1
AS
BEGIN
EXEC usp_customer;
END;
Note that in SQL Server, you should not use the "sp_" prefix for stored procedures. That is best reserved only for system stored procedures.

Ingres DB error E_PS055B CREATE PROCEDURE

E_PS055B CREATE PROCEDURE: You may not create database procedure
'proc_name'
because database procedure 'proc_2' invoked by it is dormant.
Any database procedure attempting to invoke a dormant
database procedure is itself dormant, and it is illegal
to create dormant database procedures.
I get this error when was trying to create procedure, could you please explain me what does it mean, cant find any information on it
DB Ingres version 10
From the Ingres documentation:
The DROP PROCEDURE statement removes a database procedure definition from the database. Sessions that are executing the procedure are allowed to complete before the procedure's query plan is removed from memory.
If a procedure that is executed from another procedure is removed, the
calling procedure is retained but marked dormant, and cannot be executed
until the called procedure is restored.
In summary, proc_2 calls a procedure which has been dropped. This means proc_2 will be marked as dormant (as it cannot run) until such time as the dropped procedure is replaced. Any procedures which call proc_2 will also be marked as dormant. You will need to work out which procedure that proc_2 needs has been dropped and recreate it before you can create your new procedure.

Can't execute stored procedure

I have created a stored procedure and can see it under the stored procedure node, but when trying to execute it doesn't find the procedure.
Under stored procedure node it is called dbo.CopyTable
exec CopyTable
CopyTable is undefined in red saying it does not exist. Why?
Even if I right-click on the procedure and say script stored procedure as execute to - the code it generates is underlined in red and cant find stored procedure either.
Ensure that the database selected contains the stored procedure CopyTable
USE YourDatabase
EXEC CopyTable
Try adding dbo and selecting the right database,
USE databaseName
GO
EXEC dbo.CopyTable
GO
Execute a Stored Procedure
Most likely you are just in the wrong database in the query window, you can specify the database like this:
EXEC [yourDBName].dbo.CopyTable
Reading on how to Execute a Stored Procedure
Considering your updated question:
Even if i rightclick on the procedure and say script stored procedure
as execute to - the code it generates is underlined in red and cant
find stored procedure either.
This could happen if your stored procedure is invalid. Please double-check the validity of the SPROC and ensure the tables it references exist, etc.
Try running your CREATE PROCEDURE. Highlight it, f5 it, and then make sure it runs before you call it elsewhere.
Maybe in your procedure you've accidentally cut-pasted your script name (dbo.CopyTable), say something like...
SELECT * FROM dbo.CopyTable
WHERE ClientId = #ClientId
RETURN
Then when you call your proc you get 'invalid object name dbo.CopyTable' and assume sql is having trouble finding the stored-proc ... which isn't the problem, its finding and running the proc but its actually a problem within the proc.

Stored procedures and error handling

I have 10 stored procedures.
For example -
stored procedure fetches the rows from table A
then stored procedure runs and then third...
How can I do error handling in this.. for example I have to check with if first stored procedure executed successfully run second else throw error. If first executed successfully run second stored procedure if second runs successfully run third otherwise throw error.
ALTER PROCEDURE [dbo].[MASTER_PROCEDURE] AS
EXEC QRY_STEP3
EXEC QRY_STEP_3_1_1
EXEC OQRY_STEP_3_1_1
I would add logic to each of your subsidiary stored procedures to determine whether they have succeeded or not. eg test for existence of the temporary table. Then use a return value to indicate success of the proc. Typically this would be 0 for succes and non-zero for failure.
You would then call the procs from your master proc like this
DECLARE #ReturnValue INT
EXEC #ReturnValue = QRY_STEP1
IF(#ReturnValue = 0)
BEGIN
EXEC #ReturnValue = QRY_STEP2
END
ELSE
BEGIN
--REPORT ERROR
END
Using this approach, your master proc doesnt need to know about the inner workings of each child proc, and your master proc code will be cleaner and more readable.
use ##error.. Can be done like this
ALTER PROCEDURE [dbo].[MASTER_PROCEDURE] AS
EXEC QRY_STEP3
IF ##error =0
begin
EXEC QRY_STEP_3_1_1
else
begin
print "error in proc name"
return 1
End
if (##error=0)
Begin
EXEC OQRY_STEP_3_1_1
Else
print "error in proc name"
return 1
End
END
END
First to do this correctly you should use TRY CATCH blocks in the child packages. Those should return to the calling proc if there is an error. This way you can also return an error code to the calling proc if results are unexpected, such as a temp table with zero records which is not an error but which might make the subsequent procs fail.
Next, why are you using child procs at all? Honestly this is something that is probably better done in one proc. You say for instance that you are creating temp tables in one proc that you use in subsequent procs. To do this you need global temp tables. The problem is that global temp tables are not specific to the orginal connection that called them and thus two people trying to do this simluatnaeously might have their data mixedup. Whereas if you use one proc and local temp tables that can't happen.

Is it possible to create a global stored procedure at Sql server level

I created a query that takes a database backup at certain specified location.
I want to use it as a stored procedure but this should act as a global stored procedure so that whenever this SP is called. Then database backup is taken.
It uses DB_Name() to take database backup of owner database.
Is it possible to create any such SP or Function.
I am using sql server 2005
first solution:
If you create your sp in the master database and mark it as a system object and prefix it with 'sp_' then a single copy will exist that will be shared by all databases.
and second solution from msdn:
Private and global temporary stored procedures, analogous to temporary tables, can be created with the # and ## prefixes added to the procedure name. # denotes a local temporary stored procedure; ## denotes a global temporary stored procedure. These procedures do not exist after SQL Server is shut down.
an example :
USE master
CREATE TABLE test (c1 VARCHAR(50))
INSERT test VALUES('master')
go
CREATE PROC sp_test AS
SELECT * FROM test
GO
USE northwind
CREATE TABLE test (c1 VARCHAR(50))
INSERT test VALUES('northwind')
USE pubs
CREATE TABLE test(c1 VARCHAR(50))
INSERT test VALUES('pubs')
USE pubs
EXEC sp_test --returns 'master'
USE master
EXEC sp_MS_marksystemobject sp_test
USE pubs
EXEC sp_test --returns 'pubs'
USE northwind
EXEC sp_test --returns 'northwind'
Three steps must be followed to create a "system" stored procedure that is accessible to all databases on the Server, as well as be able to run under the context of the current database when it is called.
Master Database - The stored procedure should be created in the Master database
Prefix Stored Procedure - The stored procedure name should be prefixed with sp_
Mark SP as System Object - Call sp_ms_marksystemobject to mark custom SP as a system object
Example Code Below
--Step 1, Create in master database
USE master
GO
--Step 2, Prefix with sp_ the custom proc
CREATE PROCEDURE sp_myCustomSystemProc
AS
BEGIN
PRINT 'myCustomCode'
END
GO
--Step 3, Mark as system object so proc executes in context of current db
EXEC sp_ms_marksystemobject 'sp_myCustomSystemProc'
GO
There are 3 requirement for such stored procedure
The stored procedure must be created in the master database.
The name of the stored procedure must start with “sp_“.
The stored procedure must be marked as a system object.
-- 1. Create the procedure in the master database
USE master
GO
-- 2. Create the procedure with the prefix sp_
CREATE PROCEDURE sp_[Stored_Procedure_Name]
AS
BEGIN
-- Insert the logic of your stored procedure here
END
GO
-- 3. Mark the stored procedure as a system object
EXEC sys.sp_MS_marksystemobject sp_[Stored_Procedure_Name]