bind variable substitution in oracle - plsql

The below simple procedure is suppose to provide grants to the user scott in oracle.
The value for &scott_SCHEMA is already defined in a seperate file(define_variable.sql) and the value is getting substituted correctly, but im getting the error(as specified in the bottom of the script), a help is much appreciated.
SET SERVEROUTPUT ON
declare
l_sql varchar2(3200);
begin
for i in ( select table_name as oname,'TABLE' as type from all_tables where owner='HR' AND table_name not like 'BIN$%' union all select view_name as oname,'VIEW' as type from all_views where owner='HR' and view_name not like 'BIN$%' )
loop
if i.type = 'TABLE' then
dbms_output.put_line(l_sql);
l_sql:= 'grant select,insert,update,delete on hr.'||i.oname||' to :owner with grant option';
execute immediate l_sql using '&scott_SCHEMA';
else
l_sql:= 'grant select on hr.'||i.oname||' to :owner with grant option';
end if;
end loop;
end;
/
*declare
*
ERROR at line 1:
ORA-00987: missing or invalid username(s)
ORA-06512: at line 12*

You can't use bind variables in place of identifiers (specifically schema or object names) in DDL or DML statements, they can only be used in place of value expressions.
Since you're using substitution variables, you could just place it in the sql statement itself:
l_sql:= 'grant select,insert,update,delete on hr.'||i.oname||' to &scott_SCHEMA with grant option';
and execute it with out passing it as a parameter:
execute immediate l_sql;
On a side note, your DBMS_OUTPUT line should come after you assignment to l_sql, otherwise you'll miss outputting one or more of the statements being processed. A good place would be between the assignment statement and the execute statement.

Related

How to correctly make a procedure in Pl/SQL in which I create a TABLE and use a CURSOR

The assignment I am trying to do is
"Create a procedure that places the names of all presidents who were born in one specific
state, in a temporary table. Display the contents of this table."
The procedure complies but when I try to invoke it, it gives me:
00000 - "table or view does not exist"
Error(8,5): PLS-00103: Encountered the symbol "CREATE" when expecting one of the following: begin function pragma procedure subtype type current cursor delete exists prior
I have been stuck for a while now. Does anybody know what I am doing wrong?
My code so far is:
CREATE OR REPLACE PROCEDURE stateofpresident(p_state president.state_born%TYPE)
AS
CURSOR c_state IS
SELECT *
FROM president;
BEGIN
execute immediate 'CREATE TABLE presidentFromState;
(
president_name VARCHAR2
)';
FOR r_state IN c_state LOOP
IF(p_state = r_state.state_born) THEN
execute immediate 'INSERT INTO presidentFromState VALUES(r_state.pres_name)';
commit;
END IF;
END LOOP;
execute immediate 'DROP TABLE presidentFromState';
END stateofpresident;
/
SET SERVEROUT ON
BEGIN
stateofpresident('VIRGINIA');
END;
/
SELECT *
FROM presidentFromState;
The immediate cause of your error is the semi-colon (;) at "presidentFromState;" At run fhat terminates the statement and the SQL interpreter at that point does not know what is want, the create syntax is invalid. The statement compiles because at compile time it is a properly formatted string. That is why dynamic SQL should be avoid if at all possible. Your script also has an additional error. Your last select will fail as the table presidentFromState ws not only created but also dropped in the procedure. Finally, just an FYI, the entire FOR cursor and the cursor itself is entirely unnecessary, the entire operation can be completed is one statement: Look into the structure
Insert into table_name(columns)
Select columns ...
Since this obviously an assignment or tutorial I'll leave the exact for your research.

plsq collection type definition

I was trying to do insert based on the instruction provided in this site.
I can run this example
CREATE OR REPLACE PROCEDURE test_proc (p_array_size IN PLS_INTEGER DEFAULT 100)
IS
TYPE ARRAY IS TABLE OF all_objects%ROWTYPE;
l_data ARRAY;
CURSOR c IS SELECT * FROM all_objects;
BEGIN
OPEN c;
LOOP
FETCH c BULK COLLECT INTO l_data LIMIT p_array_size;
FORALL i IN 1..l_data.COUNT
INSERT INTO t1 VALUES l_data(i);
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
END test_proc;
/
Similarly, I have changed the table name for real use as below.
CREATE OR REPLACE PROCEDURE test_proc (p_array_size IN PLS_INTEGER DEFAULT 100)
IS
TYPE ARRAY IS TABLE OF web.salesline%ROWTYPE;
l_data ARRAY;
CURSOR c IS SELECT * FROM web.salesline;
BEGIN
OPEN c;
LOOP
FETCH c BULK COLLECT INTO l_data LIMIT p_array_size;
FORALL i IN 1..l_data.COUNT
INSERT INTO t2 VALUES l_data(i);
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
END test_proc;
/
But I'm getting following error, even though the table exists and accessing from the schema I was running.
SQL> show errors
Errors for PROCEDURE :
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/4 PL/SQL: Item ignored
6/34 PLS-00201: identifier 'WEB.SALESLINE' must be declared
11/7 PL/SQL: SQL Statement ignored
16/9 PL/SQL: ORA-00942: table or view does not exist
20/5 PL/SQL: SQL Statement ignored
20/40 PLS-00597: expression 'OBJECTTABLE$' in the INTO list is of wrong
type
23/5 PL/SQL: SQL Statement ignored
23/27 PL/SQL: ORA-00904: : invalid identifier
Unless the procedure is created in the web schema, you are referring to another schema's table, and so that schema needs to give you direct permission to use it. Note that there are no roles in stored procedure.
As the WEB user:
grant select on salesline to devuser;
(or whatever schema the procedure resides in).
Typically we try to avoid hardcoding schema names and instead manage these references using synonyms, so as DEVUSER:
create or replace synonym salesline for web.salesline;
Roles come back into play if you define the procedure authid current_user (the default is authid definer), however this is generally not a good idea for procedures that perform DML.

Why do we use execute immediate in plsql?

Why do we use "Execute immediate" in plsql? I know we use it to execute dynamic sql statements. But still I'm unable to convince interviewer. Could anybody give me an exact and proper answer for this? Though I use it everyday, but still unable to explain it. One thing I know is, it's used in DML statements and to retrieve multiple rows through select statement. Please give an exact definition for using "Execute immediate".
EXECUTE IMMEDIATE enables execution of a DML or DDL statement which is held as a string and only evaluated at runtime. This enables one to dynamically create the statement based on program logic. EXECUTE IMMEDIATE is also the only way you can execute DDL within a PL/SQL block. See the Oracle Manual for a complete and thorough review of these features.
While this is a very useful facility it should be used with care. Unless there is an explicit need for dynamic sql then it is better to directly declare the sql within your PL/SQL code. This will enable Oracle to parse the SQL at compile time for validity and also reduce overhead when executing the pre-compiled statement. Also you need to be very careful to avoid SQL injection attacks when dynamically building SQL.
For example, let's say you have some kind of application where users define their company and all the work they do. If you want to make it dynamically for users, then you can give them option to create their own models(tables). Since you do not know how many tables they will have, and how many attributes this tables will have you can use execute immediate.
DDL Statement in Procedures or Anonymous PLSQL blocks
Here is an example showing how to use dynamic DDL to create, drop and re-create a table:
BEGIN
EXECUTE IMMEDIATE 'create table abcd (efgh NUMBER)';
EXECUTE IMMEDIATE 'drop table abcd';
EXECUTE IMMEDIATE 'create table abcd (efgh VARCHAR2(10))';
END;
You can use this method to execute any DDL.
Though it's not suggested to use Execute Immediate for DDL, rather Global Temp Table should be used.
To execute DML statements in Procedures or Anonymous PLSQL blocks
To execute DML commands more often than DDL. With dynamic SQL you can issue inserts, updates and deletes just as you can with static SQL:
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO abcd (efgh) VALUES (:text_string)'
USING 'ijkl';
EXECUTE IMMEDIATE 'INSERT INTO abcd (efgh) VALUES (:text_string)'
USING 'mnop';
EXECUTE IMMEDIATE 'UPDATE abcd ' ||
'SET efgh = :text_string WHERE efgh = :second_string'
USING 'qrst', 'mnop';
EXECUTE IMMEDIATE 'DELETE FROM abcd ' ||
'WHERE efgh = :text_string '
USING 'qrst';
END;
In Select queries with bind variables
As useful as DDL and DML are, a database is not very useful if you can't get your data out. You can also use execute immediate to select your data back out.
DECLARE
v_data abcd.efgh%TYPE;
v_data_row abcd%ROWTYPE;
BEGIN
EXECUTE IMMEDIATE 'SELECT efgh FROM abcd WHERE efgh = :text_string'
INTO v_data
USING 'ijkl';
DBMS_OUTPUT.PUT_LINE( 'Column Variable: ' || v_data );
EXECUTE IMMEDIATE 'SELECT * FROM abcd WHERE efgh = :text_string'
INTO v_data_row
USING 'ijkl';
DBMS_OUTPUT.PUT_LINE( 'Row Variable: ' || v_data_row.efgh );
END;
INFO: Column Variable: ijkl
INFO: Row Variable: ijkl

Execute Immediate

Can any one help me to to find out the error.
create or replace procedure sample
is
begin
DECLARE AGG_COLUMNS VARCHAR2(2000);
BEGIN
EXECUTE IMMEDIATE 'SELECT COLUMNS FROM COLUMN_NAMES' INTO AGG_COLUMNS;
END;
begin
EXECUTE IMMEDIATE
'CREATE TABLE NEW_BW_COLUMN_ROW_CELL_JOIN AS
(
SELECT *
FROM BW_COLUMN_ROW_CELL_JOIN
PIVOT
(
MAX(STRING_VALUE)
FOR COLUMN_NAME IN ('||AGG_COLUMNS||')))';
END;
end;
Error(9,5): PL/SQL: Statement ignored
Error(17,36): PLS-00201: identifier 'AGG_COLUMNS' must be declared
Thanks
The immediate error is that the local variable AGG_COLUMNS is declared in the first nested PL/SQL block. That means that it is out of scope as soon as the first nested block completes. You cannot, therefore, use it in the second nested PL/SQL block. You can fix that by declaring the local variable in your procedure's declaration section. You can also get rid of the nested PL/SQL blocks
create or replace procedure sample
is
AGG_COLUMNS VARCHAR2(2000);
begin
EXECUTE IMMEDIATE 'SELECT COLUMNS FROM COLUMN_NAMES' INTO AGG_COLUMNS;
EXECUTE IMMEDIATE
'CREATE TABLE NEW_BW_COLUMN_ROW_CELL_JOIN AS
(
SELECT *
FROM BW_COLUMN_ROW_CELL_JOIN
PIVOT
(
MAX(STRING_VALUE)
FOR COLUMN_NAME IN ('||AGG_COLUMNS||')))';
end;
This should remove the immediate compilation error. I'm not sure, though, whether that resolves all your problems.
COLUMN_NAMES isn't a table in a default Oracle install. This would have to be something that you created in order for this code to run.
If you did create the COLUMN_NAMES table and COLUMNS stores a comma-separated string, then there is no need to use dynamic SQL to query the table. You can do a simple SELECT ... INTO.

Use a declared variable in a SELECT statement

I'm using Oracle 10g and need to use a variable in a where clause of a SELECT; eg.
DECLARE
v_blah NUMBER;
BEGIN
v_blah := 13;
SELECT * FROM PEOPLE p WHERE p.LuckyNumber = v_blah;
END;
but am getting an error saying
PLS-00428: an INTO clause is expected in this SELECT statement
It seems to work fine in a DELETE or INSERT statement, so I'm not sure why it wouldn't work here.
The correct syntax is:
DECLARE
v_blah NUMBER := 13;
v_people_rec PEOPLE%ROWTYPE;
BEGIN
SELECT * INTO v_people_rec FROM PEOPLE p WHERE p.LuckyNumber = v_blah;
END;
The select statement in PL/SQL requires a place where store the query result. In this example the place is v_people_rec variable.
The example above expects exactly one row to be returned. In other cases it will throw exceptions NO_DATA_FOUND or TOO_MANY_ROWS.
That isn't anything to do with your parameter, it is because you're executing your code as a procedural block of code so it doesn't allow you to select to nothing.
What do you want to do with the result of the query? Display it to the screen? If so, select it to a cursor, iterate through and use dbms_output.

Resources