Thursday, March 22, 2012

a view in a package for oracle

Ho do I create a view inside a package.Use EXECUTE IMMEDIATE:

BEGIN
...
EXECUTE IMMEDIATE 'create view v_emp as select * from emp';
END;

But why would you want to do that?|||I have to create install scripts for these, because they need to go into various schemas. along with functions and procedures. So I wanted to bundle them all up. That way a package can stay in the schema for future reference. Or can we store scripts on the schema.
Why, is this in-efficient?|||No, I thought maybe you were creating views dynamically from your application. Normally packages are used for code that will be run many times. What you are doing is unusual, but harmless.|||Hi, I am unable to figure out how to invoke this view from the package specification. here is what I have

CREATE OR REPLACE PACKAGE ABC IS
end;
/

I know something has to identify the view in the header but I cant figure out how to do that.

CREATE OR REPLACE PACKAGE BODY ABC AS
BEGIN
EXECUTE IMMEDIATE
'CREATE OR REPLACE VIEW ABCD
(LISTNAME, CODE, DESCRIPTION)
AS
SELECT
translate(CODELKUP.LISTNAME,"^`"""," ") LISTNAME,
translate(CODELKUP.CODE,"^`"""," ") CODE,
translate(CODELKUP.DESCRIPTION,"^`"""," ") DESCRIPTION
FROM
CODELKUP';
END;
/|||You need to create a procedure in the package like this:

CREATE OR REPLACE PACKAGE ABC IS
procedure create_view;
end;
/

CREATE OR REPLACE PACKAGE BODY ABC AS

procedure create_view is
begin
EXECUTE IMMEDIATE
'CREATE OR REPLACE VIEW ABCD
(LISTNAME, CODE, DESCRIPTION)
AS
SELECT
translate(CODELKUP.LISTNAME,"^`"""," ") LISTNAME,
translate(CODELKUP.CODE,"^`"""," ") CODE,
translate(CODELKUP.DESCRIPTION,"^`"""," ") DESCRIPTION
FROM
CODELKUP';
end;

END;
/

You will have problems with those quotes though. Oracle uses only single quotes (') to delimit text strings.

No comments:

Post a Comment