Storing oracle error/query result in a table - sql

Does anyone know if this is possible?
Example:
CREATE TABLE XPTO (
this_is_an_error_because_there_is_no_data_type
);
Query Result:
"**Error starting at line X in command:
blablabla
SQL ERROR: ORA-02263**"
Is it possible to save the query result in a table?
I'm making an "installation" script with CREATE tables/procedures, GRANT's simple stuff, but I want to know if it's possible to save in a table the errors that may popup during the installation of my tables or whatever it is in there (in my install file .sql).
Any help, ideas, work around/s, would be appreciated. Thanks guys!

In SQL*Plus this can be done with SQL*Plus error logging.
Enable error logging:
SQL> set errorlogging on
Run the script:
SQL> CREATE TABLE XPTO (
2 this_is_an_error_because_there_is_no_data_type
3 );
this_is_an_error_because_there_is_no_data_type
*
ERROR at line 2:
ORA-00972: identifier is too long
At the end, all errors are stored in the table SPERRORLOG.
SQL> select * from sperrorlog;
USERNAME
------------------------------------------------
TIMESTAMP
------------------------------------------------
SCRIPT
------------------------------------------------
IDENTIFIER
------------------------------------------------
MESSAGE
------------------------------------------------
STATEMENT
------------------------------------------------
JHELLER
25-JAN-16 01.43.13.000000 PM
ORA-00972: identifier is too long
CREATE TABLE XPTO (
this_is_an_error_because_there_is_no_data_type
)
As Tony Andrews said in the comment, SPOOL is the most common method for doing this. But that's because very few SQL*Plus scripts are fully automated. If you are aiming for true automation you'll need something like SQL*Plus error logging or whenever sqlerror exit failure to detect errors.

Related

Firebird with .net driver - drop table if exists

I'm new to Firebird and I'm testing a few things to check out the differences between Fb and SQlite (and the .net driver).
I am trying to do a drop table if exists followed by the creation of a table. In Sqlite I am able to do this by:
command.CommandText = #"DROP TABLE IF EXISTS Persons; CREATE TABLE Persons (
PersonID int,
LastName text,
FirstName text,
Address text,
City text); ";
command.ExecuteNonQuery();
However in Firebird the same query fails. I've read that this is not possible to use IFs directly in Firebird SQL, so I've tried to use:
command.CommandText = #"
EXECUTE BLOCK AS
BEGIN IF EXISTS
(SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = 'Persons')
THEN DROP TABLE Persons; END CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); ";
command.ExecuteNonQuery();
But it fails also with the following error:
Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column
27
Can you please help me on this? I've tried to find more info on the web that could help me, but did not have any luck.
Firebird's SQL syntax doesn't have a drop table if exists, instead use recreate table. recreate table will try to drop the table if it exists before creating it. The syntax of recreate table is - other than recreate instead of create - the same as create table.
Your attempt to use execute block fails for two reasons:
You cannot execute two statements together as a command. If you want to execute a script of multiple statements, you'll need to execute each statement individually or use the FbScript class which will parse the script and execute the individual statements for you.
Even if you execute these statements individually, it will still fail, because PSQL (the stored procedure language used in execute block) does not allow execution of DDL. You can use execute statement to circumvent this limitation, but it is better not to do that. In that way you could also address the previous point by executing both - using execute statement - within the execute block.
Alternatively you could just drop the table unconditionally and catch (and ignore) the resulting exception.

Oracle Error using Toad:

Since a couple of days I can't execute CREATE and TRUNCATE SQL statement using Oracle 12 and Toad 12, ONLY if I terminate my statements with a semicolumn. I've always used the same connections, user and tables, but now I receive these errors while querying:
CREATE TABLE W_TEST_01
AS SELECT *
FROM CFG_FLOW
WHERE 1 = 2;
ORA-00933: SQL command not properly ended
CREATE TABLE W_TEST_01 (PIPPO NUMBER);
ORA-00922: missing or invalid option
TRUNCATE TABLE W_TEST_01;
ORA-03291: Invalid truncate option - missing STORAGE keyword
Can someone please help me?
Try with parentheses, like this:
CREATE TABLE W_TEST_01
AS (SELECT * FROM CFG_FLOW WHERE 1 = 2);
And if it still does not work, try to see the CTAS Tips
I advise you to take a look at the following link
Hope this can help!

Insert rows from a table in another database

How can rows be inserted into a table from a table in a remote database?
We currently have a stored procedure which does this using a database link. However, we are having to delete the link because our company policy doesn't allow their usage.
begin
...
execute immediate 'insert into '|| table_name
|| ' (select * from schema_name.'|| table_name ||'#link)';
...
end;
I'm unable to use the COPY command as it appears to be not recognized.
COPY FROM username/pwd#SID
insert into table_name using (select *
from table_name);
Error report:
SQL Error: ORA-00926: missing VALUES keyword
00926. 00000 - "missing VALUES keyword"
*Cause:
*Action:
According to this SQL Plus page, COPY command is now obsolete.
Your Query syntax is slightly wrong, and you just need to specify INSERT/REPLACE/CREATE .. and INTO is NOT needed.
COPY is not obsolete, but it ends up with some encoding issues.
You would use the line continuation character to get around that.
COPY
FROM username/pwd#SID
TO username/pass#SID2
insert
table_name
using
select * from schema_name.table_name;
You can also, download the table data into a text file, and use SQL*Loader to load the data into the another Database.
I prefer the SQL*Loader option! Since maintenance is easy!
For download,
- you can either use SPOOL with delimiter option as mentioned Here
- Write a ProC/PLSQL to output the data into a File (Method 4 in ProC, OR select column names from dba_columns))

ORA-01031: insufficient privileges when bulk loading data from one schema to another via SQL*Loader

I am trying to INSERT data into another schema's table via SQL Loader. Here is the control file:
LOAD DATA
INFILE *
INTO TABLE globalref01.dw_stg_holiday_extract
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
( ric_trd_exch,
cntry_cde,
holiday_date "TO_DATE (:holiday_date, 'YYYYMMDD')",
holiday_desc,
trd,
stl
)
Notice I'm inserting into table SCHEMA.TABLE_NAME. Since I'll be sqlldr'ing from the schema depotapp01, I'll run the following command on globalref01:
GRANT INSERT ON dw_stg_holiday_extract TO depotapp01;
Check and see if it works:
SELECT * FROM user_tab_privs_recd WHERE table_name = 'DW_STG_HOLIDAY_EXTRACT' AND owner = 'GLOBALREF01';
That confirms I have INSERT privs from depotapp01:
GLOBALREF01 DW_STG_HOLIDAY_EXTRACT GLOBALREF01 INSERT NO NO
Now, when I try to execute the sqlldr command, I get ORA-01031: insufficient privileges:
SQL*Loader: Release 10.2.0.4.0 - Production on Mon Feb 3 09:41:43 2014
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Control File: /db/platform/eq/sparc_SunOS_5.6/depot/2.0/DWRef/cfg/uat/2.1/base_config/holiday_calendar.ctl
Data File: /export/data/depotdw/DWRef/data/oats/holiday_calendar.dat
Bad File: /export/data/depotdw/DWRef/data/oats/holiday_calendar.err
Discard File: /export/data/depotdw/DWRef/data/oats/holiday_calendar.dsc
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 200000
Bind array: 64 rows, maximum of 1000000 bytes
Continuation: none specified
Path used: Conventional
Table GLOBALREF01.DW_STG_HOLIDAY_EXTRACT, loaded from every logical record.
Insert option in effect for this table: INSERT
TRAILING NULLCOLS option in effect
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
RIC_TRD_EXCH FIRST * | CHARACTER
CNTRY_CDE NEXT * | CHARACTER
HOLIDAY_DATE NEXT * | CHARACTER
SQL string for column : "TO_DATE (:holiday_date, 'YYYYMMDD')"
HOLIDAY_DESC NEXT * | CHARACTER
TRD NEXT * | CHARACTER
STL NEXT * | CHARACTER
SQL*Loader-929: Error parsing insert statement for table GLOBALREF01.DW_STG_HOLIDAY_EXTRACT.
ORA-01031: insufficient privileges
So my question is, now that I know I have INSERT privileges, what other privileges do I need to grant in order for this to work?
I think you also need the SELECT privilege in most cases because SQL*Loader performs checks before inserting.
In particular, the default loading option is INSERT:
INSERT
This is SQL*Loader's default method. It requires the table to
be empty before loading. SQL*Loader terminates with an error if the
table contains rows.
In order to check that the table is empty, the account used by SQL*Loader needs the SELECT privilege. You might not need it if loading as APPEND instead. Conversely, if you use the REPLACE or TRUNCATE option, you'll need additional privileges.

Informix SQL - What is wrong with this simple stored procedure &| trigger syntax?

IBM Informix Dynamic Server Version 11.50.FC6
I am trying to execute a simple stored procedure from within an update trigger. Together, they are used to update a field with the current timestamp when another field in the same row is updated.
Table sp_test:
id (serial int, unique, not null, primary key)
stat (char(1), not null, default="A")
add_date (date, not null, default today)
upd_date (date, null)
The stored procedure code is:
create procedure upd_row_date_proc (cid int)
update sproc_trig_rec set upd_date = current where id = cid;
end procedure;
This executes fine and creates the routine, but the trigger I am trying to implement on updates is not working.
The trigger code is:
create trigger upd_row_date_trig
update of stat on sproc_trig_rec
after (execute procedure upd_row_date_proc(id));
I've tried a bunch of syntax variations, but cannot get it to work.
I usually get my error on the ( char of the 3rd line. Here's the error code:
201: A syntax error has occurred.
Error in line 3
Near character position 0
Does anyone know what I'm doing wrong in the syntax of the trigger? Could this type of updating be defined in the creation of the table, or do I need to accomplish it by doing it the way described above?
Thanks for any help
This finally worked for me
create trigger ken_trig
update of stat on sproc_trig_rec
referencing old as ken_pre_upd
for each row (execute procedure ken_proc(ken_pre_upd.id));