refresing Oracle MV remotely - oracle

I'm trying to refresh Oracle Materialized View on remote DB using DB Link name. everything working fine once I'm using static db link name like
DBMS_MVIEW.REFRESH#QA_LINK('tablename_mv','?')
BUT
when I want to get it (db link name) as a variable in many ways
v_dblink :='qa';
DBMS_MVIEW.REFRESH#v_dblink('AVBROCHURESTATS','?');
always getting an errors like
ORA-06550: line 57, column 15:
PLS-00352: Unable to access another database 'V_DBLINK'
ORA-06550: line 57, column 15:
PLS-00201: identifier 'DBMS_MVIEW#V_DBLINK' must be declared.
Please advice.

DB link names must always be explicit; there's no variable substitution in the DDL. The best you could do would be to use dynamic SQL to build the execution string on the fly:
v_dblink := 'QA'
EXECUTE IMMEDIATE 'DBMS_MVIEW.REFRESH#' || v_dblink ||'(''AVBROCHURESTATS'',''?'')';

the problem was related to the same invoking , becuse
with execute immediate you need to put that statement into a begin end; block
like
execute immediate ' begin DBMS_MVIEW.REFRESH#'|| v_dblink||'(''AVBROCHURESTATS'',''?''); end;';

Related

How to use this execute immediate create table to insert values in table

I am using execute immediate to create table and use this table into my code to insert some values when i run this code .it gives me error that table and view does not exist.why?. this code has ability to create table because when i use drop and create table command with existing table by using execute immediate then my code works fine and when i just remove drop table syntax, my code does not work, Please help me to clear my concept about dynamic SQL-----------
SET serveroutput ON
DECLARE
ddl_table VARCHAR2(200);
r_emp SYS.ODCINUMBERLIST := SYS.ODCINUMBERLIST();
v_array SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST('ACCT_ID',
'PARENT_ACCT_ID',
'CUST_ID',
'ACCT_NAME',
'BILLING_CYCLE_TYPE',
'PAID_FLAG',
'BILL_DELIVER_METHOD');
BEGIN
ddl_table := 'CREATE TABLE test123(
v_column VARCHAR2(50),
v_count NUMBER
)';
EXECUTE IMMEDIATE ddl_table;
DBMS_OUTPUT.ENABLE;
FOR i IN 1 .. v_array.COUNT LOOP
r_emp.EXTEND;
EXECUTE IMMEDIATE 'SELECT COUNT(*)
FROM account_profile
WHERE NOT REGEXP_LIKE(' ||v_array(i) || ',''[A-Za-z0-9.]'')'
INTO r_emp(i);
IF r_emp(i) <> 0 THEN
DBMS_OUTPUT.PUT_LINE(v_array(i) || ': ' || r_emp(i));
INSERT INTO test123 VALUES (v_array(i), r_emp(i));
END IF;
END LOOP;
END;
/
Error report -
ORA-06550: line 24, column 17:
PL/SQL: ORA-00942: table or view does not exist
Your problem is that the annonymous block is pre-validated (pre-compiled) before it is valid. Oracle will check all objects in use before executing it. Since you are creating test123 dynamically it doesn't exist so your insert statement fails.
You can instead, use an EXECUTE IMMEDIATE command to also insert the data on your test123 table.
The way you use the EXECUTE IMMEDIATE for an insert command is either concatenating the parameters or preparing them, I prefer the later. Like this
execute immediate 'insert into test123 values (:param1,:param2)' using v_array(i), r_emp(i);
Here is the official documentation for the EXECUTE IMMEDIATE command.
Though it shows how it works and explain the usage of it, it doesn't particularly answer you direct question on the comments.
So
can you explain :param1,:param2
Those are called "binding" parameters that will be replaced by the variables used after the using statement. It doesn't matter their name only the order in which they appear on the string. The number of parameters within the dynamic string needs to match the number of parameters after the using statement.
why we use it with : colon and what are these
The colon : there is just to make it easier for the parser to know what to replace and where when using the variables you provided after the using statement
Translating that execute immediate it would be something like this:
... values (:param1,:param2)' using v_array(i), r_emp(i)
Hey "execute immediate" whenever you see :param1 please replace it with the content of the variable I'm providing as v_array(i) and whenever you see :param2 please replace it with the content of the variable I'm providing as r_emp(i)

Object is invalid PLSQL Procedure

I use a dataset that has billing information:
Table Image
And I want to create a procedure that gives information about a specific bill by giving an invoice id
CREATE OR REPLACE PROCEDURE print_information(
invoiceID varchar(11);
) IS
p_smtable supermarket%ROWTYPE;
BEGIN
SELECT * INTO p_smtable FROM SUPERMARKET
WHERE invoice_id = invoiceID;
DBMS_OUTPUT.PUT_LINE(p_smtable.brancj || p_smtable.city);
END;
The compilation is successful.
Compile
begin
print_information('750-67-8428');
end;
But I get an error that says:
Error starting at line : 12 in command -
begin
print_information('750-67-8428');
end;
Error report -
ORA-06550: line 2, column 1:
PLS-00905: object SYSTEM.PRINT_INFORMATION is invalid
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The code in the screenshot is different to the version in your question, but both have an error at line 2. The parameter should be varchar2 or supermarket.invoice_id%type, with no semicolon.
Also, we normally use a p_ prefix for parameters, not variables. (There are other conventions, but whatever you use, p_ for a variable is just confusing.)
A fixed version might be:
create or replace procedure print_information
( p_invoice_id supermarket.invoice_id%type )
as
l_market supermarket%rowtype;
begin
select * into l_market
from supermarket
where invoice_id = p_invoice_id;
dbms_output.put_line(l_market.brancj || ' '|| l_market.city);
end;
Whatever tool you are using for development, you need to become familiar with how to display compilation errors.
brancj might be a typo for branch.
I've assumed you want a space between branch and city, otherwise the output would be something like TottenhamLondon rather than Tottenham London.
In Oracle a schema belongs to one user, created with the CREATE USER command. An Oracle Database is the entire thing (all users, all data, storage, memory, processes, everything) so you would not normally create one for something like this.

Oracle basic multi-statement block

In SQL Server, multiple statements in SSMS can be performed. In Oracle (using toad), I don't know why I receive certain errors doing the same thing. For example, I assume an Oracle requirement is to place them in a block, but I still get the following:
DECLARE
v_datetime TIMESTAMP := SYSDATE;
BEGIN
insert into sometable_log values (v_datetime, 'this is a test ',1);
select * from sometable_log where event_dt = v_datetime;
END;
produces:
[Error] Execution (5: 1): ORA-06550: line 4, column 1: PLS-00428: an
INTO clause is expected in this SELECT statement
Why would I need to use an into clause? Can someone please help me understand what this is?
Thank you!
When executing the block of code, Oracle expects that the select command should have some code-like effect. That's why it wants you to add the into clause.
To do what you need, just move the select statement outside of the PL/SQL begin/end block.

Why can't execute SCHEMA_NAME.PACKAGE_NAME.PROCEDURE, Oracle

I have a schema A, Package B and Procedure C. B is in A schema and C is in B's Package Body.
It works fine when I say:
Begin
Exec B.C;
END;
But it throw an error when I say:
Begin
Exec A.B.C;
END;
Error report:
ORA-06550: line 2, column 12:
PLS-00302: component 'B' must be declared
ORA-06550: line 2, column 4:
PL/SQL: Statement ignored
I log in as A so it's in A's schema.
and
SELECT * FROM user_OBJECTS WHERE OBJECT_NAME = 'B';
shows package and package body both valid.
I had the same problem and found the issue. I will write here the answer to close this problem and help other people.
In my case, my user A have execute privilege on a procedure B(just a procedure, not a package, but it's the same). When the user tries to run:
Begin
Exec A.B;
END;
Get the error:
ERROR at line 2:
ORA-06550: line 2, column 7:
PLS-00302: component 'B' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
Problem: That's because a public synonym with name A was created in the database. This is an old database and I am just the DBA, not the developer, but in this case the developer was an uninspired developer. He used 4 types of objects with the same name: user, table, tablespace and public synonym. A public synonym named A in front of a table named A.
Solution: Because you don't know exactly who is using that public synonym I had to found another solution instead of deleting the public synonym. I created a private synonym for that procedure. Now the user can skip the owner of procedure in the execution code and ignore the public synonym. This problem appears in Oracle Database 10.2.0.4.
Begin
Exec B;
END;
PL/SQL procedure successfully completed.
Conclusion: Never use a public/private synonym with the name of the schema.
Hope to help someone. If I didn't made myself clear, please leave a comment.
Fix syntax error in the package, this is a generic PL/SQL compile error message.
Check error points (line 2, column 12) in the PL/SQL where the syntax error occurred, correct it and then try recompiling your code.
Component 'B' must be declared.
After that grant EXECUTE_CATALOG_ROLE to allow user 'A' execute privileges for packages and procedures in the data dictionary.
grant EXECUTE_CATALOG_ROLE to A;

ORA-06550 Why I can't create index inside the if-else in Oracle

I not family with PL/SQL. Can anyone explain why I can't do the following?
BEGIN
IF TRUE THEN
CREATE INDEX TestIndex ON SomeTable (SomeColumn);
END IF;
END;
I would get the following error:
Error report:
ORA-06550: line 3, column 5:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin case declare exit for goto if loop mod null pragma
raise return select update while with
<<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The only way I can by pass this error is do dynamic sql:
BEGIN
IF TRUE THEN
EXECUTE IMMEDIATE 'CREATE INDEX TestIndex ON SomeTable (SomeColumn)';
END IF;
END;
Oracle doesn't allow DDL in a PL/SQL block as static SQL so you've identified the only workaround (well, technically, you could use DBMS_SQL rather than EXECUTE IMMEDIATE but you'd still be dealing with dynamic SQL).
I don't know that there is any technical reason that they couldn't allow DDL in static SQL. But since 99% of the time you shouldn't be doing DDL in a stored procedure-- creating objects is something that would almost exclusively be done when you're doing a deployment rather than at runtime-- forcing people to use dynamic SQL makes developers pause to consider whether they're really in that 1% of cases where such a thing makes sense.

Resources