Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i am trying to create a table using below code.
set serveroutput on;
DECLARE
cursor c1 is select '''create table demo1 (demo varchar2(100))''' c2 from dual;
testvar c1%rowtype;
BEGIN
open c1;
fetch c1 into testvar;
close c1;
execute immediate testvar.c2;
END;
but it is giving
ORA-00900: invalid SQL statement
ORA-06512: at line 8
00900. 00000 - "invalid SQL statement"
What is wrong in this code.
You need nothing more than except these 3 lines of code if you really want to do it dynamically using a PLSQL block. No need for selecting from dual .
declare
vsql varchar2 (100) := 'create table demo1 (demo varchar2(100))';
begin
execute immediate vsql;
end;
Note: It does not makes any sense selecting create table statement from a select. If you have any other specific requirement, please mention so that people can help you here.
Btw, you block will work simplly once you remove the extra ' as mentioned by William
set serveroutput on;
DECLARE
cursor c1 is select 'create table demo1 (demo varchar2(100))' c2 from dual;
testvar c1%rowtype;
BEGIN
open c1;
fetch c1 into testvar;
close c1;
execute immediate testvar.c2;
END
;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
hello guys can you help me here
CREATE TRIGGER operation
AFTER UPDATE ON COMPTE
for each row
BEGIN
IF :old.solde > :new.solde THEN
INSERT INTO HISTORY VALUES(NUMEROCOMPTE,DATESOLDE,'Retrait');
end if;
IF :old.solde < :new.solde THEN
INSERT INTO HISTORY VALUES(NUMEROCOMPTE,DATESOLDE,'Versement');
end if;
end;
/
this is what i get
3/1 PL/SQL: SQL Statement ignored
3/41 PL/SQL: ORA-00984: Column Not Allowed
6/1 PL/SQL: SQL Statement ignored
6/41 PL/SQL: ORA-00984: Column Not Allowed
Errors : check compiler log
i'm new so i dont know what to do
Do as Ken commented.
Problem is with insert statements (both of them) and values you're trying to insert into the history table. You can't insert column names as such - you have to precede their names with :new or :old pseudorecord identifier, just as you did in if.
I don't know which values (old or new) you want to store into the history table so it is just my guess, but you should know it and you'll therefore be able to fix
it.
Sample tables:
SQL> create table compte (solde number, numerocompte number, datesolde date);
Table created.
SQL> create table history (muberocompte number, datesolde date, description varchar2(20));
Table created.
Trigger:
SQL> CREATE TRIGGER operation
2 AFTER UPDATE ON COMPTE
3 for each row
4 BEGIN
5 IF :old.solde > :new.solde THEN
6 INSERT INTO HISTORY VALUES(:old.NUMEROCOMPTE, :old.DATESOLDE, 'Retrait');
7 ELSIF :old.solde < :new.solde THEN
8 INSERT INTO HISTORY VALUES(:new.NUMEROCOMPTE, :new.DATESOLDE, 'Versement');
9 END IF;
10 end;
11 /
Trigger created.
SQL>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My query says Compiled Successfully, but cannot find the result. Can anyone advise? New to PLSQL. Thanks
This is how I understood the problem.
You didn't specify what you exactly did (what kind of a PL/SQL code is it?), but - it could be a stored procedure:
SQL> create or replace procedure p_test as
2 begin
3 dbms_output.put_line('Hello');
4 end;
5 /
Procedure created.
or perhaps an anonymous PL/SQL block:
SQL> begin
2 dbms_output.put_line('Hello');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL>
Both of these are supposed to display the "Hello" message. Obviously, neither of them did. I don't know what your PL/SQL code does, but - if it also displays something, enable the output. In SQL*Plus, SQL Developer and some other tools that support it, you'd run
SQL> set serveroutput on
If it is a stored procedure you created, you have to execute it to see the result:
SQL> exec p_test
Hello --> "Hello" is here!
PL/SQL procedure successfully completed.
SQL>
Or, if it is an anonymous PL/SQL block, you don't have to do anything special:
SQL> begin
2 dbms_output.put_line('Hello');
3 end;
4 /
Hello --> "Hello" is here!
PL/SQL procedure successfully completed.
SQL>
See if this helps. If not, explain the problem a little bit better and we'll try to assist.
I assume you are trying to create a function/procedure/package although you haven't shown us your code.
Try this:
SELECT *
FROM all_objects
WHERE owner = (SELECT user FROM dual)
AND object_name = '[Your Object Name]'
;
It should show you the schema under which your object was created.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
My table name is ARBITER and one of the columns is CREATE_DATE. I want to delete the data older than 4 months.
How to rewrite the procedure correctly? So I have written this:
procedure RemoveOldData(CREATE_DATE) as
BEGIN
delete from ARBITER
where trunc(sysdate) < add_months(trunc(sysdate),-4);
END;
but i got this error
Error(91): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: begin end function pragma procedure
This procedure will produce the specific outcome you require:
create or replace PROCEDURE RemoveOldData IS
BEGIN
delete from ARBITER
where create_date < add_months(trunc(sysdate),-4);
END;
/
Here is a LiveSQL demo (OTN account required).
The problem is what you intend with this parameter
PROCEDURE RemoveOldData (ARBITER in varchar2)
Is that supposed to be a cut-off date? A table name?
Alternatively, you may use months_between function(if exactly period of four months should be included, add an equality sign before 4 also) :
PROCEDURE RemoveOldData
BEGIN
delete arbiter where months_between(trunc(sysdate),create_date)>=4;
END;
/
create or replace procedure RemoveOldData(CREATE_DATE in varchar2)
as
begin
delete from CREATE_DATE
where trunc(sysdate) < add_months(trunc(sysdate),-4);
exception
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
end;
/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i'm doing the follwing question
Managing Locked Accounts – The utility should be able to identify each locked
account that was locked because of invalid login attempts. The utility should further
unlock accounts that have been locked for more than a week.
----------------------------------------my code------------------------------------------
procedure Managing_Locked
is
num int;
V_SQL varchar(50);
begin
for x in (select USERNAME,LOCK_DATE,ACCOUNT_STATUS,PROFILE from DBA_USERS where ACCOUNT_STATUS ='LOCKED(TIMED)')
loop
dbms_output.put_line('USERNAME: '|| x.USERNAME);
dbms_output.put_line('LOCK_DATE: '|| x.LOCK_DATE );
dbms_output.put_line('ACCOUNT_STATUS: '||x.ACCOUNT_STATUS);
dbms_output.put_line('PROFILE: ' ||x.PROFILE);
dbms_output.put_line('*********************');
select ((sysdate - x.LOCK_DATE)) into num from dual;
if(num>7)
then
V_SQL := 'ALTER USER'|| x.USERNAME ||'ACCOUNT UNLOCK';
dbms_output.put_line(x.USERNAME||' UNLOCK');
Execute immediate(V_SQL);
end if;
end loop;
end;
error 00940. 00000 - "invalid ALTER command"
i getting that error and i don't see anything wrong with the alter command
Just in order to catch each possible status the where clause should be this one:
WHERE ACCOUNT_STATUS IN (
'LOCKED(TIMED)',
'EXPIRED & LOCKED(TIMED)',
'EXPIRED(GRACE) & LOCKED(TIMED)')
or
WHERE ACCOUNT_STATUS LIKE '%LOCKED(TIMED)'
add a space before the word account.
The user name is missing with the word "account", so the command you issue is "alter user usernameaccount unlock" ...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to insert million of records into a file using oracle and without using a loop or java code.
while browsing I found something as util_file in oracle which is used to record rows into file but was unable to use it. can some please help me in understanding or writing the code to write the resultset returned by a select query into a file or even a procedure would even work.
I tried below procedure it run successfully but doesnot create file with data
create or replace procedure familydetails
( p_file_dir IN varchar2, p_filename IN varchar2 )
IS v_filehandle utl_file.file_type;
cursor family Is
select * from fmly_detl
begin
v_filehandle :=utl_file.fopen('C:\tempfolder','sqltest.txt','W');
utl_file.new_line(v_filehandle);
for fmly IN fmly_detl LOOP
utl_file.putf(v_filehandle,'family %s details:s\n',fmly.last_name,fmly.first_name,fmly.indv_id);
end loop;
utl_file.put_line(v_filehandle,'END OF REPORT');
UTL_FILE.fclose(v_filehandle);
end familydetails;
1) if you use sqlplus in unix.. Here is a simple solution, put the below as a script_name.ksh and execute it (ksh script_name.ksh)
sqlplus -s user_id/password#sid << ! >> ~/sql_output.txt
set feedback off;
set pages 0;
set linesize 20000;
set arraysize 1000;
set tab off;
--Your Query, with proper padding , or comma seprated
--Eg Select employee_id||'|'||employee_name||'|'||employee_age
-- from employee;
!
2) If you use a IDE like SQL Developer or TOAD, you can execute the Query and Export it.
3) But for PL/SQL
test_dir mentioned below, is a directory in the host machine , accessible to Oracle. It should be listed in *ALL_DIRECTORIES* dictionary table.
DECLARE
fileHandler UTL_FILE.FILE_TYPE;
cursor emp_cursor is select employee_id,employee_name,employee_age FROM employee;
TYPE emp_type IS TABLE OF emp_cursor%ROWTYPE INDEX BY PLS_INTEGER;
l_emp_type emp_type;
v_fetch_limit NUMbER := 10000;
BEGIN
fileHandler := UTL_FILE.FOPEN('test_dir', 'sql_output.txt', 'W',max_linesize => 4000);
open emp_cursor;
LOOP
FETCH emp_cursor BULK COLLECT INTO l_emp_type LIMIT v_fetch_limit;
EXIT WHEN l_emp_type.COUNT < v_fetch_limit;
// Used to control the fetch size.
FOR I IN 1..l_emp_type.COUNT LOOP
UTL_FILE.PUTF(fileHandler, '%s|%s|%s',l_emp_type(I).employee_id,
l_emp_type(I).employee_name,
l_emp_type(I).employee_age);
END LOOP;
UTL_FILE.FFLUSH(fileHandler);//Flush the buffer to file
END LOOP
UTL_FILE.FCLOSE(fileHandler);
IF(emp_cursor%ISOPEN) THEN
emp_cursor.close();
END IF;
EXCEPTION
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, 'ERROR: Invalid PATH FOR file.');
IF(emp_cursor%ISOPEN) THEN
emp_cursor.close();
END IF;
END;
/
Finally, copy it from the UTL_FILE directory in Server.
The directory may be a NAS mount too. Just the Oracle need to have write access to it.
4) Like a PL/SQL, a Pro*C program, or Any OCI interface too will work! Generally, Options 3 and 4 gives you good control in the process you do!
Good Luck!
EDIT: Added improvements over fetch size and flushing