Stored Procedure

How do I create a stored procedure in DB2 that will drop a table ?

Thanks
Los Angeles1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave FordSoftware Developer / Database AdministratorCommented:

I suspect that there are more details to your question than the one sentence you specified, but the following might get you started:

CREATE procedure DropIt ()
  LANGUAGE SQL
  modifies sql data
BEGIN
   drop table deleteme cascade;
   commit;
END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
momi_sabagCommented:
how about something like

CREATE PROCEDURE drop_table ( IN table_name varchar(128))
LANGUAGE SQL
MODIFIES SQL DATA

P1: BEGIN
-- Declare variables
DECLARE DUMMY INT;

-- Declare Cursor
DECLARE myCursor CURSOR  FOR
SELECT 1 x
FROM SYSCAT.tables
WHERE table_name = ?
WITH UR;

open myCursor using table_name;

fetch myCursor into dummy;

if (sqlcode = 0) then
 execute immediate 'drop table ' || table_name
end if

END P1
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi Greensburo,

I'm assuming that this question is a follow-up to your previous?  If so, you might consider moving the DROP TABLE statement back to the command prompt.  There's often a philosophical disagreement about running DDL from a stored procedure.

Run the DROP TABLE statement as a single command from the CLI before you run the rest of your script.

db2 'DROP TABLE sometable'

Unix shells can easily test the return value so that the script continues whether or not the DROP statement succeeded.  The same with DOS scripting, though the processes is a bit different.


Kent


0
Dave FordSoftware Developer / Database AdministratorCommented:
Alternately,

CREATE PROCEDURE DropIt2 (IN p_tableName CHAR(100))
  LANGUAGE SQL MODIFIES SQL DATA
BEGIN
  DECLARE v_sql CHAR(256);
  SET v_sql = 'DROP table ' || trim(p_tableName) || ' cascade';
  PREPARE v_statement FROM v_sql;
  EXECUTE v_statement;
END

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.