Tuesday, August 31, 2010

Run PL/SQL Fragments Directly in SqlPlus*

BEGIN
FOR i IN (SELECT table_name FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || i.table_name;
END LOOP;
COMMIT;
END;
Ok so you want to run the fragment above that you got from a forum post somewhere and only have access to the command line SqlPlus* utility to do so.
You add more semi-colons, press the enter key repeatedly and hope it magically parses the snippet and determines that you must want to execute the contents of the BEGIN ... END block right?

Unfortunately it doesn't work that way :). You need to tell it to parse the contents of the buffer by adding the forward slash / to the end. Voila!
BEGIN
FOR i IN (SELECT table_name FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || i.table_name;
END LOOP;
COMMIT;
END;
/

Please see this post for truncating/deleting all tables in a schema using a stored procedure in PL/SQL.

No comments:

Post a Comment