Tuesday, September 16, 2008

Bind Variables in SQL Code


@bind
---------------------------------------------------------
-- Bind variables can be declared in your SQL*Plus script
---------------------------------------------------------
VARIABLE s_table_name varchar2(30)

---------------------------------------------------------
-- Preface a bind variable with a colon to reference it
-- in a PL/SQL block.
---------------------------------------------------------
BEGIN
:s_table_name := 'EMPLOYEES';
END;
/

---------------------------------------------------------
--Bind variables persist until you exit SQL*Plus, so
--they can be referenced by more than one PL/SQL block.
---------------------------------------------------------
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(:s_table_name);
END;
/
@bind2
------------------------------------------------------
--Bind variables can even be referenced by SQL queries
------------------------------------------------------

SELECT Table_Name, Tablespace_Name, Status
FROM user_tables
WHERE table_name = :s_table_name;


Note: Use 'Print' to display the value:
Print :s_table_name

No comments:

Post a Comment