Home » SQL & PL/SQL » SQL & PL/SQL » Accessing v$mystat gives table or view does not exist
Accessing v$mystat gives table or view does not exist [message #463442] Thu, 01 July 2010 06:35 Go to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
Hi,

I am using the sid of v$mystat to create a unique filename in my pl/sql procedure.

I have granted access to v$mystat to the user that is accessing it from system user as:

SQL>GRANT SELECT ON V_$MYSTAT TO ar;
Grant succeeded.

SQL> commit;

Commit complete.

now when i login as user ar and do a select on v$mystat it works fine:

SQL> select sid from v$mystat WHERE ROWNUM = 1;

SID
----------
290

However, when i do the same from my PL/SQL procedure it throws an error saying :

SQL> @FILECREATE
53 /

Warning: Function created with compilation errors.

SQL> show errors
Errors for FUNCTION FILECREATE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
22/8 PL/SQL: SQL Statement ignored
22/35 PL/SQL: ORA-00942: table or view does not exist

My PL/SQL function can be found as an attachment.


I am unable to understand what is wrong . Can anyone please help ?
Re: Accessing v$mystat gives table or view does not exist [message #463444 is a reply to message #463442] Thu, 01 July 2010 06:42 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
You need a direct privilege the user. Privileges granted to roles are not considered when creating a procedure.

Regards
Ved
Re: Accessing v$mystat gives table or view does not exist [message #463445 is a reply to message #463444] Thu, 01 July 2010 06:50 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
According to the OP, that's not the problem - the grant was to the user.
Re: Accessing v$mystat gives table or view does not exist [message #463446 is a reply to message #463444] Thu, 01 July 2010 06:50 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
I dont have the system admin privileges. I instead tried using a sequence to replace the sid in the filename creation.Code is now:

CREATE OR REPLACE FUNCTION FILECREATE (
p_source IN SYS_REFCURSOR,
p_filename IN VARCHAR2,
p_directory IN VARCHAR2
) RETURN dump_ntt
PIPELINED
PARALLEL_ENABLE (PARTITION p_source BY ANY) AS

TYPE row_ntt IS TABLE OF VARCHAR2(32767);
v_rows row_ntt;
fileID UTL_FILE.FILE_TYPE;
v_buffer VARCHAR2(32767);
-- v_sid NUMBER;
v_name VARCHAR2(128);
v_lines PLS_INTEGER := 0;
c_eol CONSTANT VARCHAR2(1) := CHR(10);
c_eollen CONSTANT PLS_INTEGER := LENGTH(c_eol);
c_maxline CONSTANT PLS_INTEGER := 32767;

BEGIN
-- SELECT sid INTO v_sid FROM v$mystat WHERE ROWNUM = 1;
v_name := p_filename || '_' || TO_CHAR(v_sid.nextval) || '.txt';
fileID := UTL_FILE.FOPEN(p_directory, v_name, 'w', 32767);

LOOP
FETCH p_source BULK COLLECT INTO v_rows LIMIT 100;
FOR i IN 1 .. v_rows.COUNT LOOP

IF LENGTH(v_buffer) + c_eollen + LENGTH(v_rows(i)) <= c_maxline THEN
v_buffer := v_buffer || c_eol || v_rows(i);
ELSE
IF v_buffer IS NOT NULL THEN
UTL_FILE.PUT_LINE(fileID, v_buffer);
END IF;
v_buffer := v_rows(i);
END IF;
END LOOP;
v_lines := v_lines + v_rows.COUNT;
EXIT WHEN p_source%NOTFOUND;
END LOOP;
CLOSE p_source;

UTL_FILE.PUT_LINE(fileID, v_buffer);
UTL_FILE.FCLOSE(fileID);

PIPE ROW (dump_ot(v_name, v_lines, v_sid));
RETURN;

END FILECREATE;

and i have created the sequence as:

SQL> CREATE SEQUENCE v_sid
2 MINVALUE 1
3 MAXVALUE 999999999999999999999999999
4 START WITH 1
5 INCREMENT BY 1
6 CACHE 20;

Sequence created.

SQL> commit;

Commit complete.

But now it throws me the following error:

17:16:42 SQL> @FILECREATE
17:16:57 50 /

Warning: Function created with compilation errors.

Elapsed: 00:00:00.12
17:16:58 SQL> show errors
Errors for FUNCTION FILECREATE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
22/7 PL/SQL: Statement ignored
22/52 PLS-00357: Table,View Or Sequence reference 'V_SID.NEXTVAL' not allowed in this context

46/7 PL/SQL: Statement ignored
46/42 PLS-00357: Table,View Or Sequence reference 'V_SID' not allowed in this context

Seems like the logic is wrong or i am not using it the right way. Can you please suggest how i can generate a unique file name in my PL/SQL function ?
Re: Accessing v$mystat gives table or view does not exist [message #463447 is a reply to message #463446] Thu, 01 July 2010 07:00 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
What I think was that privilege was given to role ar

Regards
Ved
Re: Accessing v$mystat gives table or view does not exist [message #463448 is a reply to message #463446] Thu, 01 July 2010 07:04 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
What version of oracle you are using?
You can not assign V_SID.NEXTVAL DIRECTLY in Oracle version prior to 11g Like this

v_name := p_filename || '_' || TO_CHAR(v_sid.nextval) || '.txt';

use select seq.nextval into v_1 dual in oracle version prior to 11g


Regards
Ved

[Updated on: Thu, 01 July 2010 07:09]

Report message to a moderator

Re: Accessing v$mystat gives table or view does not exist [message #463451 is a reply to message #463448] Thu, 01 July 2010 07:07 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
I am using oracle 10.2 . And you are right in saying privilege was given to the user ar.

If i cant use the sequence directly, can you suggest me how to do it then ?
Re: Accessing v$mystat gives table or view does not exist [message #463452 is a reply to message #463451] Thu, 01 July 2010 07:11 Go to previous messageGo to next message
Its_me_ved
Messages: 979
Registered: October 2009
Location: India
Senior Member
select seq.nextval into v_1 from dual ;


Regards
Ved

[Updated on: Thu, 01 July 2010 07:11]

Report message to a moderator

Re: Accessing v$mystat gives table or view does not exist [message #463468 is a reply to message #463452] Thu, 01 July 2010 08:07 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
This helped ! Thanks a lot for all your help Smile
Re: Accessing v$mystat gives table or view does not exist [message #463577 is a reply to message #463468] Fri, 02 July 2010 01:08 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
HI,

After updating the script with the below syntax for the sequence, it seemed to work properly and create the file also correctly. But now the file is not getting created. I am using the following command to invoke the function :

SELECT *
FROM TABLE(
FILECREATE(
CURSOR(SELECT
CMD_STRING AS csv
FROM batchfileEntry s
WHERE file_id = 'RequestFile.txt'),
'utl_file_parallel_pipelined',
'TESTDIR'
)) nt;

here CMD_STRING is a CLOB type.

The PL/SQL function is below:


CREATE OR REPLACE FUNCTION FILECREATE (
p_source IN SYS_REFCURSOR,
p_filename IN VARCHAR2,
p_directory IN VARCHAR2
) RETURN dump_ntt
PIPELINED
PARALLEL_ENABLE (PARTITION p_source BY ANY) AS

TYPE row_ntt IS TABLE OF CLOB;
v_rows row_ntt;
fileID UTL_FILE.FILE_TYPE;
v_buffer VARCHAR2(32767);
v_1 NUMBER;
v_name VARCHAR2(128);
v_lines PLS_INTEGER := 0;
c_eol CONSTANT VARCHAR2(1) := CHR(10);
c_eollen CONSTANT PLS_INTEGER := LENGTH(c_eol);
c_maxline CONSTANT PLS_INTEGER := 32767;

BEGIN
-- SELECT sid INTO v_sid FROM v$mystat WHERE ROWNUM = 1;
select v_sid.nextval into v_1 from dual ;
v_name := p_filename || '_' || TO_CHAR(v_1) || '.txt';
dbms_output.put_line('SID value is:' || TO_CHAR(v_1));
fileID := UTL_FILE.FOPEN(p_directory, v_name, 'w', 32767);
dbms_output.put_line('FILE CREATED');

LOOP
FETCH p_source BULK COLLECT INTO v_rows LIMIT 100;
FOR i IN 1 .. v_rows.COUNT LOOP

IF LENGTH(v_buffer) + c_eollen + LENGTH(v_rows(i)) <= c_maxline THEN
v_buffer := v_buffer || c_eol || v_rows(i);
ELSE
IF v_buffer IS NOT NULL THEN
UTL_FILE.PUT_LINE(fileID, v_buffer);
END IF;
v_buffer := v_rows(i);
END IF;
END LOOP;
v_lines := v_lines + v_rows.COUNT;
EXIT WHEN p_source%NOTFOUND;
END LOOP;
CLOSE p_source;

UTL_FILE.PUT_LINE(fileID, v_buffer);
UTL_FILE.FCLOSE(fileID);

PIPE ROW (dump_ot(v_name, v_lines, v_1));
RETURN;

END FILECREATE;


on running this the process just hangs:

11:28:06 SQL> SELECT *
FROM TABLE(
FILECREATE(
CURSOR(SELECT
CMD_STRING AS csv
FROM batchfileEntry s
WHERE file_id = 'RequestFile.txt'),
'utl_file_parallel_pipelined',
'TESTDIR'
)) nt;11:28:13 2 11:28:13 3 11:28:13 4 11:28:13 5 11:28:13 6 11:28:13
7 11:28:13 8 11:28:13 9 11:28:13 10

I have around 200,000 records that will get returnd to the select statement.

Can anyone tell me what is worng ??
Re: Accessing v$mystat gives table or view does not exist [message #463581 is a reply to message #463577] Fri, 02 July 2010 01:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version with 4 decimals.

Regards
Michel
Re: Accessing v$mystat gives table or view does not exist [message #463585 is a reply to message #463581] Fri, 02 July 2010 01:27 Go to previous message
archana485
Messages: 22
Registered: June 2010
Junior Member
Hi,

Apologies abt the formatting, hope this is ok. My oracle version is 10.2.0.1.0

CREATE OR REPLACE FUNCTION FILECREATE (
                    p_source    IN SYS_REFCURSOR,
                    p_filename  IN VARCHAR2,
                    p_directory IN VARCHAR2
                    ) RETURN dump_ntt
                      PIPELINED
                      PARALLEL_ENABLE (PARTITION p_source BY ANY) AS
  
       TYPE row_ntt IS TABLE OF CLOB;
       v_rows    row_ntt;
       fileID    UTL_FILE.FILE_TYPE;
       v_buffer  VARCHAR2(32767);
       v_1     NUMBER;
       v_name    VARCHAR2(128);
       v_lines   PLS_INTEGER := 0;
       c_eol     CONSTANT VARCHAR2(1) := CHR(10);
       c_eollen  CONSTANT PLS_INTEGER := LENGTH(c_eol);
       c_maxline CONSTANT PLS_INTEGER := 32767;
 
   BEGIN
       -- SELECT sid INTO v_sid FROM v$mystat WHERE ROWNUM = 1;
       select v_sid.nextval into v_1 from dual ;
       v_name := p_filename || '_' || TO_CHAR(v_1) || '.txt';
       dbms_output.put_line('SID value is:' || TO_CHAR(v_1));
       fileID := UTL_FILE.FOPEN(p_directory, v_name, 'w', 32767);
       dbms_output.put_line('FILE CREATED');
  
       LOOP
         FETCH p_source BULK COLLECT INTO v_rows LIMIT 100;
	 FOR i IN 1 .. v_rows.COUNT LOOP
              IF LENGTH(v_buffer) + c_eollen + LENGTH(v_rows(i)) <= c_maxline THEN
                v_buffer := v_buffer || c_eol || v_rows(i);
              ELSE
                  IF v_buffer IS NOT NULL THEN
                    UTL_FILE.PUT_LINE(fileID, v_buffer);
                  END IF;
                  v_buffer := v_rows(i);
              END IF;
         END LOOP;
              v_lines := v_lines + v_rows.COUNT;
         EXIT WHEN p_source%NOTFOUND;
       END LOOP;
       CLOSE p_source;
       UTL_FILE.PUT_LINE(fileID, v_buffer);
       UTL_FILE.FCLOSE(fileID);
       PIPE ROW (dump_ot(v_name, v_lines, v_1));
       RETURN;
END FILECREATE;


And the output is:

11:33:37 SQL> SELECT *
11:52:28 2 FROM TABLE(
11:52:30 3 FILECREATE(
11:52:39 4 CURSOR(SELECT CMD_STRING AS csv FROM batchfileEntry
11:52:49 5 WHERE file_id = 'RequestFile.txt'),
11:52:52 6 'utl_file_parallel_pipelined',
11:52:55 7 'TESTDIR')) nt;

Thanks and Regards,
Archana
Previous Topic: Can you use a sequence in a prepared statement?
Next Topic: Merge into table name using cursor
Goto Forum:
  


Current Time: Sun May 31 08:18:30 CDT 2026