Real reason for adding DROP in CREATE Stored procedure - stored-procedures

I just wanted to know what is the best practice to create and execute a stored procedure.
I have seen like below:-
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Foo')
DROP PROCEDURE Foo
GO
CREATE PROCEDURE dbo.Foo
But I am thinking when we execute this stored procedure, it will drop the stored procedure and create a new one. But if there is error in the create stored procedure syntax, it won't recreate the stored procedure, right? So as a result our existing stored procedure is deleted and new stored procedure is not created. So what is the real reason for adding DROP here?

The reason is so that your scripts can be run in an idempotent way - they can be run as many times as needed, with the same result. Namely, your database will have the stored procedure you desire. Your procedure will be created, and dropped beforehand if needed. If you didn't do this, then you'd need separate drop and create scripts.
If you're concerned that your scripts have errors, well, fix them. Run your scripts a few times, and fix any problems that arise. The effort is worth not having to maintain separate sets of scripts.

BECAUSE it is execute in line by line manner so when it create procedure and then error comes and again you recreate that procedure then it gives error because it is already generated in your database with error code....
so for this it want recreate that procedure again.....
you can use alter instead of create....
Hope this help you...

Related

Creating a Snowflake warehouse in a stored procedure changes the current warehouse

I'm trying to write a procedure that creates users, roles and warhouse. This procedure is executed with a high privilege user with ACCOUNTADMIN role
USE ACCOUNT_ADMIN;
USE WAREHOUSE PROVISIONER;
CREATE or replace PROCEDURE TEST()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
snowflake.execute({ sqlText: "CREATE OR REPLACE WAREHOUSE test;"});
return "";
$$
;
Now if I query the current warehouse:
SELECT CURRENT_WAREHOUSE();
I get PROVISIONER
If I now call the procedure
CALL TEST();
The warehouse has changed
SELECT CURRENT_WAREHOUSE();
Now returns TEST
Is this the normal behaviour? This is an issue because the warehouse is changed outside of the procedure, and I cannot use the "USE WAREHOUSE" statement inside a procedure.
Is there another way to create WAREHOUSE from a procedure without changing the current warehouse?
Yes, this is expected. See the docs for background information.
One way to work around it would be to get the current warehouse at the beginning of your stored procedure, and set it again as the current one at the end of the stored procedure.

Avoiding hard coding Schema in DB2/400 Stored Procedure

I'm creating Stored Procedures to replace Legacy app programs for an IBM i. I'm calling the stored procedure from a Java Web App. I'm using the jt400 JDBC driver
My JDBC URL is jdbc:as400://myhost/;libraries=*LIBL,MYLIB;prompt=false
The stored procedures can call stored procedures
The initial stored procedure call completes normally if it does not make further stored procedure calls
If the stored procedure makes a call to other stored procedures it fails with
com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0204] MY_SP in MYLIB type *N not found.
If I hard code a schema in the stored procedure call statement, the call completes normally.
I want to have the called stored procedures use the same schema as the caller
You need to SET PATH = "MYLIB"
When I am using SQuirreL to call a stored procedure, I need to use the SET PATH statement to get it to find the stored procedure. I don't know if that is because my library list is bad or what, but the current schema is not used to find an unqualified stored procedure.
I actually had this same problem, the stored procedure uses your job description library list. You need to edit that you can use TAATOOL CHGLBLJOBD. I am not in front of an iSeries at the moment but I believe the command was either EDTJOBDLIB or EDTJOBDLIBL WRKJOBDLIBL. It is some variation of that.

Informix stored procedure

When creating a stored procedure in Informix, it does not throw an error even if the related table does not exist..
I guess the reporting level is pretty high so how can I change it?
That is the way Informix is designed to work.
If, when you run the procedure, the table still does not exist, then you will get a more or less appropriate runtime error. But the mere fact that a table does not exist at the time the procedure is created is quite deliberately not viewed as an error; the table may be created by the time the procedure is used.
There is no setting that I know of to change this behaviour.

Value Displays when Running Stored Procedure in SSMS but not in SSRS

I have a stored procedure that uses table variables to create a query and runs perfectly when executing in SQL Server Management Studio. However, the column referring to this table variable does not display when running the stored procedure in Query Designer.
I have used this method on many other reports without issue, but cannot figure out why the value will display in SSMS and not in SSRS.
After a fresh nights sleep, I realized it was how a parameter was being passed to the Sub Stored Procedures. The main stored procedure had a where clause that contained a "LIKE #Parameter" but the Sub Stored Procedure contained an "= #Parameter", so when a "%" was passed into the parameter the main Stored Procedure returned results, but nothing was displayed from the Sub Stored Procedure.
Simply just a case of making my job harder on myself...

How i can put a result set from a stored procedure in a temporary table in DB2

The title is very descriptive i think... My scenario is the next. I need to put the result of a result set (for example a result set with 6 columns and variable rows) from a stored procedure in some temporary table to make some operations over this new table.
I find some examples in the web but nothing in DB2...
The big problem is how to populate that new table with the restult set of a called stored procedure
DECLARE GLOBAL TEMPORARY TABLE probably accomplishes what you want. You can create this type of temporary table inside a stored procedure. It is visible only to the current session and is cleaned up for you when the session ends.

Resources