Abort inserting a record in a table using a trigger
Hi there,
Is there any way to abort inserting a record in a table using a trigger?
For full details, I have the following table ("myTable"):
BSC INTEGER NOT NULL,
BTS VARCHAR2(20) NOT NULL,
INFO1 INTEGER,
INFO2 INTEGER
myTable_PK = PRIMARY KEY (BSC,BTS)
I have also a stored procedure that imports a data from text file and inserts them to the specified table (using UTL_FILE package). The stored procedure works great.
But the thing that in the text file itselft it might be (due to third-parity report generation bug) that the primary key will be violated or the BSC/BTS field has null value. In such case I just want to ignore the insertion statement using a trigger.
Thanks
Ok Jens, could you tell me what exception could I use?
Below a protion of my StoredProcedure.
CREATE OR REPLACE PROCEDURE update_myTable() IS
FHANDLE UTL_FILE.FILE_TYPE;
BSC INTEGER;
BTS VARCHAR2(20);
INFO1 INTEGER;
INFO2 INTEGER;
BEGIN
FHANDLE := UTL_FILE.FOPEN('LOG_FILE_DIR',FILENAME,'R',4000);
LOOP
UTL_FILE.GET_LINE(FHANDLE,STR);
-- Process the line STR and generates BSC, BTS, INFO1, and INFO2 values
EXECUTE IMMEDIATE 'INSERT INTO myTable VALUES(:1,:2,:3,:4)' USING BSC,BTS,INFO1,INFO2;
END LOOP;
EXCEPTION WHEN NO_DATA_FOUND THEN UTL_FILE.FCLOSE(FHANDLE);
END UPDATE_R205BTS;
Remember that I am already using an exception with NO_DATA_FOUND to indicate the end of file then closing it.
Thanks for your reply
Similar Messages
-
How do you insert new records into multiple tables using the same unique primary key?
I’ve created a PHP site and MySQL server using a free app called XAMPP. I have successfully created a form in Dreamweaver that will write data to a (name) table in the SQL database. Here’s my question: How do you write to two (or more) tables in the same database and pass the same primary key to both tables? In the SQL database, I defined the first field as ID and set it as the primary key with auto update. So, when you insert a new record, it creates a unique primary key for that record. In my form, I’m capturing info that needs to be stored to two tables at the same time; a Name table and Address table. Since the Name and Address tables use the ID field as the primary key, I believe I will need to pass the ID value from the Name table to the insert of the Address table to insure they both have the same primary key, right?
No. You probably need the primary key from one table to be a foreign key in the other tables. In any case, I believe you can use two methods to obtain the auto generated key. First with SQL:
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
And the other using a PHP function:
http://us3.php.net/mysql_insert_id -
How i can Insert selected records in database table using check box.
Hi Friends,
I have non database block, which displayed multiple records, now I add a Check Box to this block and now I want to insert the records in a database table which I checked. So when I press a button, all the checked records are inserted in the table.
Please send me the code of this solution.
Thanks in advance.
ShahzadI have almost the exact scenario but instead of inserting into the DB, I want to find the Checked records and process them in PL/SQL script.
I currently have a cursor that looks for all the checked records. It's only getting the first record, so I figured I need to loop through the block looking for checked records.
Also can I save them to a temp or PL/SQL table to process later? -
Inserting Multiple Records into two table
I want to insert records from a ADF swing form into two tables. In the first table the primary key is generated by a trigger and then I need to retrieve the primary id generated and then insert multiple records in another table using the primarykey obtained from the first table as foreign key.
How to do this ?User,
If you're using ADF Business components, have a read on the DBSequence data type. If you have two VO's linked by a view link, and the FK is a DBSequence type, all this happens for you out-of-the-box.
Hope this helps,
john -
Insert multiple records into a table(Oracle 9i) from a single PHP statement
How can I insert multiple records into a table(Oracle 9i) from a single PHP statement?
From what all I've found, the statement below would work if I were using MySQL:
insert into scen
(indx,share,expire,pitch,curve,surface,call)
values
(81202, 28, 171, .27, 0, 0, 'C' ),
(81204, 28, 501, .25, 0, 0, 'C' ),
(81203, 17, 35, .222, 0, 0, 'C' ),
(81202, 28, 171, .27, 2, 0, 'C' ),
(81204, 28, 501, .20, 0, 1, 'C' ),
(81203, 28, 135, .22, 1, 0, 'C' )
The amount of records varies into the multiple-dozens. My aim is to utilize the power of Oracle while avoiding the i/o of dozens of single-record inserts.
Thank you,
WillYou could look at the INSERT ALL statement found in the documentation here:
http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_913a.htm#2133161
My personal opinion is that you probably won't see any benefit because under the hood I think Oracle will still be doing single row inserts. I could be wrong though.
The only way to confirm was if you did a test of multiple inserts vs an INSERT ALL, that is if the INSERT ALL met your requirements.
HTH. -
Inserting rows in a database table using JDBC
Hi,
I am trying to insert a row in a table using the follwing code:
String Query_String = "Insert into Table_Name (Field_1, Field_2, Field_3)" +
"Values (Value_1, Value_2, Value_3)";
stmt.executeUpdate (Query_String);
Field_1 is a Date field, Field_2 is a String, and Field_3 is an integer.
Any of the fields could be null. If the user does not enter a value for any
one of these fields what would be the values for
Value_1, Value_2, or Value3 in the Query_String?
Thanks.Hi,
I am trying to insert a row in a table using the
follwing code:
String Query_String = "Insert into Table_Name
(Field_1, Field_2, Field_3)" +
"Values (Value_1,
"Values (Value_1, Value_2, Value_3)";
stmt.executeUpdate (Query_String);
Field_1 is a Date field, Field_2 is a String, and
Field_3 is an integer.
Any of the fields could be null. If the user does not
enter a value for any
one of these fields what would be the values for
Value_1, Value_2, or Value3 in the Query_String?
Thanks.Keep in mind that each database server probably has their own way of storing date values. For now, I'm going to assume that you're going to be using MySQL.
// Initialize variables to null
String Value_1 = "";
String Value_2 = "";
String Value_3 = "";
try
// Get Date
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Value_1 = dateFormat.format(date);
// Get String value
Value_2 = "some string";
// Get Integer value
Value_3 = String.valueOf("2");
String query = INSERT INTO T_NAME VALUES('" + Value_1 +"', '" + Value_2 + "', '" + Value_3 + "');
int iUpdated = stmt.executeUpdate(query);
catch(Exception e)
// Catch Code
System.out.println("Number of records updated: " + iUpdated);
This will just enter empty string values if the strings are empty.
Is this what you wanted to know?
If not, provide a bit more information and I'll try to help as best as I can -
Triggers to insert the record in a table
I have two table 1. Holiday 2. Attendance.
When I insert the record in holiday table for his
advance holiday with empid, the same time I want to insert it attendance table
automatically for the same date using a trigger
Insert into attendance (empid,date,holiday) values (20078,07/10/2014,1). If holiday column value 1 represent holiday marked,
0 represent holiday not marked. The same thing can happen vice versa
If employee mark his current attendance as holiday through attendance,
it should be inserted into holiday table
automatically using triggers.
Insert into Holiday (empid,date,holiday) values (20078,06/08/2014,1). If holiday column value 1 represent holiday marked,
0 represent holiday not marked. The same thing can happen vice versa
Please I am looking for your help , how I can make it using triggers
to insert both table in two different ways of options.
Regards
Pol
polachanHi polachan,
According to your description, if you want to synchronize the data between the holiday table and the attendance table while inserting records into holiday table, you need to create a trigger on the holiday table, please try the following syntax.
use <databasename>
go
create trigger Tr_holiday
on holiday
for insert
as
declare @empid varchar(20),
@date datetime,
@holiday int
select @empid = empid, @date=date, @holiday=holiday from inserted
declare @qty int
select @qty =count(*) from attendance where empid=@empid and date=@date and holiday=@holiday
if @qty<1
begin
insert into attendance
select i.empid,
i.date,
i.holiday
from inserted i
end
Meanwhile, if you want to insert the record into the holiday table when holiday is updated to 1 in the attendance table, you need to create another trigger on the attendance table, please try the following syntax.
use <databasename>
go
create trigger Tr_attendance
on attendance
for update
as
declare @holiday int
if update (holiday)
begin
select @holiday=holiday from inserted
if @holiday=1
begin
insert into dbo.holiday
select empid,
date,
holiday
from inserted
end
end
For more details about creating triggers in SQL Server, please review this article:
CREATE TRIGGER (Transact-SQL).
Thanks,
Lydia Zhang -
Inserting new records into database table at runtime
Hi all ,
How to insert new records into database table at runtime on click update?
Thanks.Hi Sasikala,
Just for your understanding am giving a sample code snippet which you can use to read the contents of your Table UI element & save the data on to your database. Suppose you have a button up on pressing which you want to read the data from your screens table & save on to the database then you can proceed as shown below:
1) Obtain the reference of your context node.
2) Fetch all the data present in your table into an internal table using methods of if_wd_context_node
3) Use your normal ABAP logic to update the database table with the data from your internal table
In my example I have a node by name SFLIGHT_NODE and under this I have the desired attributes from SFLIGHT. Am displaying these in an editable table & the user would press up on a push button after making the necessary changes to the tables data. I would then need to obtain the tables information & save on to the database.
data: node_sflight type ref to if_wd_context_node,
elem_sflight type ref to if_wd_context_element,
lt_elements type WDR_CONTEXT_ELEMENT_SET,
stru_sflight type if_main=>element_sflight_node,
it_flights type if_main=>elements_sflight_node.
" navigate from <CONTEXT> to <SFLIGHT_NODE> via lead selection
node_sflight_node = wd_context->get_child_node( name = 'SFLIGHT_NODE' ).
lt_elements = node_sflight->get_elements( ).
" Get all the rows from the table for saving on to the database
loop at lt_elements into elem_sflight.
elem_sflight->get_static_attributes( importing static_attributes = stru_sflight ).
append stru_sflight to it_flights.
endloop.
" Finally save the entries on to the database
modify ZSFLIGHT99 from table it_flights.
if sy-subrc eq 0.
endif.
However a word of caution here.... SAP doesn't ever recommend directly modifying the database through an SQL query. You would preferably make use of a BAPI for the same. Try go through Thomas Jung's comments in [here|modify the data base table which is comming dynamiclly;.
Regards,
Uday -
Error in inserting a record in to table
hi..
i created a trigger....when i am trying to insert new record in to table i am getting the fallowing error...plz kindly help me out from this...
SQL> create or replace trigger secure_emp
2 before insert on employees begin
3 if(to_char('sysdate','DY') in ('SAT','SUN')) or
4 (to_char('sysdate','HH24:MI') not between '8:00' and '18:00') then
5 raise_application_error(-20500,'error');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into employees(employee_id,last_name,first_name,email,hire_date,job_id,salary,department
_id)
2 values (300,'smith','jack','SJACK',sysdate,'IT_PROG',4500,60);
insert into employees(employee_id,last_name,first_name,email,hire_date,job_id,salary,department_id)
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SCOTT.SECURE_EMP", line 2
ORA-04088: error during execution of trigger 'SCOTT.SECURE_EMP'Yeah. Whenever Oracle sees TO_CHAR('string_literal1', 'string_literal2), it implicitly converts it to TO_CHAR(TO_NUMBER('string_literal1'), 'string_literal2). That's why an error in trigger occurs.
SQL> explain plan for
2 select null
3 from dual
4 where dummy = to_char('A', 'DY');
Explained
SQL> @utlxpls
Cannot SET MARKUP
PLAN_TABLE_OUTPUT
Plan hash value: 397561404
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 2 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| DUAL | 1 | 2 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("DUMMY"=TO_CHAR(TO_NUMBER('A'),'DY'))
13 rows selected -
How to insert a record in ROOSPRMSC table? (accidentally deleted)
Calling an InfoPackage in BW causes short dump, while fixing the short dump issue, accidentally
deleted the "ROOSPRMSC" table entries.
Could you tell me how to insert a record in "ROOSPRMSC" table???Hi Senthil,
Regards,
Phani Raj Kallur
Message was edited by: Phani Raj Kallur -
Inserting multiples rows into a table using function or procedure..
How do i insert multiples rows into a table using function or procedure?
Please provide me query..Use FORALL bulk insert statement...
eg:
procedure generate_test_data as
type cl_itab is table of integer index by pls_integer;
v_cl_itab cl_itab;
type cl_vtab is table of varchar2(25) index by pls_integer;
v_cl_vtab cl_vtab;
type cl_dtab is table of date index by pls_integer;
v_cl_dtab cl_dtab;
begin
for i in 1.. 100 loop
v_cl_itab(i):= dbms_random.value(1,1000);
v_cl_vtab (i):=dbms_random.string('a',20);
v_cl_dtab (i):=to_date(trunc(dbms_random.value(2453737, 2454101)),'j');
end loop;
forall i in v_cl_itab.first .. v_cl_itab.last
execute immediate 'insert into test_order values( :n, :str , :dt ) ' using v_cl_itab(i), v_cl_vtab (i), v_cl_dtab (i);
commit;
end; -
Best Practice to save record in a table with appropriate trigger
Hi,
At the same time, five users are inserting client information in a client table through Oracle form. Client table has the following fields:
CLIENT ID
CLIENT NAME
CLIENT ADDRESS
CLIENT ID is generating automatically by calling a procedure. In this procedure I am using MAX function to get maximum value of CLIENT ID. After that, I store newly generated CLIENT ID in a DATA BLOCK item say :MASTER.CLIENT ID.
The problem is that all five users do get same MAX value(suppose 40) of CLIENT ID at the same time, and oracle form will surely throw an exception at the time of insertion the record in client table. CLIENT ID is PK and it is a member of MASTER DATA BLOCK.
I hope all above will clearly illustrate the problem, further please guide, can PRE-INSERT trigger may handle this problem efficiently ? , if so, then how ? ...
Thanks,Hello,
Welcome to the forum!
CLIENT ID is generating automatically by calling a procedure.So, in which trigger you are calling that procedure?
After that, I store newly generated CLIENT ID in a DATA BLOCK item say :MASTER.CLIENT ID. I would guess that you are using the code generation procedure in the WHEN-CREATE-RECORD trigger block-level. Because this trigger normally intialize the value for new record insertion. If no then specify.
further please guide, can PRE-INSERT trigger may handle this problem efficiently ? ,Yes, PRE-INSERT will work without any problem.
if so, then how ? ... Because, PRE-INSERT will fetch the MAX number from the table at the time of save not at the time of creation. So, suppose five user will enter record at the same time but when they will save then PRE-INSERT will get the max sequence number. And it will never create problem. Because in five user's insertion sure there will be some time difference. So, max number can be get easily without any problem.
-Ammad -
Insertion a record in a table having columns of different charsets using OLEDB
My development environment -
Database -> Microsoft SQL Server
2008 R2
OS -> Windows Server
2008 R2
Database Charset -> Chinese_PRC_CI_AS (Windows
936)
Operating System Charset -> Chinese
Below table is having varchar fields with different charsets.
create
table dbo.tcolcs1 (
c1 int
not
null
primary
key,
c2 varchar(30)
collate SQL_Latin1_General_Cp1_CI_AS ,
c3 varchar(30)
collate Chinese_PRC_CI_AS
I want to insert below record using OLEDB APIs provided by Microsoft. Just for information, character '0x00C4' does not
belong to Windows 936 codepage.
insert
into dbo.tcolcs1
values (10,
NCHAR(0x00C4), NCHAR(0x4EBC))
Code snippet -
DBPARAMBINDINFO bind_info
memset(&bind_info,
0, sizeof(DBPARAMBINDINFO));
bind_info.pwszDataSourceType = L"DBTYPE_VARCHAR";
bind_info.wType = DBTYPE_STR;
I have bound the varchar field with DBTYPE_STR. I can see that my code is not inserting Latin1 character (0x00C4) correctly
into the table. The code always inserts a blank character into Latin1 column (c2) and 0x4EBC into Chinese column (c3).
Later, I changed the binding from DBTYPE_STR to DBTYPE_BYTES as below -
bind_info.pwszDataSourceType = L"DBTYPE_BINARY";
bind_info.ulParamSize =
0;
bind_info.wType = DBTYPE_BYTES;
With the above change, I observed that OLEDB is converting hex value to string. It is inserting 0x00C4 as 'C4' and 0x4EBC
as '4EBC'. I also tried with adding 'AutoTranslate=no' in driver connection string, but it did not help. How can I insert above record with OLEDB in the above table ?
Thanks in advance.Did you try making fields as unicode?
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
How to insert some records in one table and some records in another table
Interview question
how insert records in two tables by using trigger
CREATE or REPLACE TRIGGER Emp_Ins_Upd_Del_Trig
BEFORE delete or insert or update on EMP
FOR EACH ROW
BEGIN
if UPDATING then
UPDATE emp2
SET
empno = :new.empno,
ename = :new.ename
--, job = :new.job
--, mgr = :new.mgr
--, hiredate = :new.hiredate
, sal = :new.sal
--, comm = :new.comm
--, deptno = :new.deptno;
sdate = :new.sdate,
edate = :new.edate
end if;
if INSERTING then
INSERT INTO emp2
VALUES
( :new.empno
, :new.ename
--, :new.job
--, :new.mgr
--, :new.hiredate
, :new.sal
--, :new.comm
--, :new.deptno
new.sdate,
new.edate);
end if;
if DELETING then
DELETE FROM emp2
WHERE empno = emp2.empno;
end if;
END;
it is working fine but he wants to insert some specific litimit on one table and some specified limit of records in one ..
In this senerio can i insert records by use count of records...
please help me..Can you be more specific on the "Limit"
Conditional insert can be used in this case. -
Inserting Multiple Rows into Database Table using JDBC Adapter - Efficiency
I need to insert multiple rows into a database table using the JDBC adapter (receiver).
I understand the traditional way of repeating the statement multiple times, each having its <access> element. However, I am just wondering whether this might be performance-inefficient, as it might insert records one by one.
Is there a way to ensure that the records are inserted into the table as a block, rather than record-by-record?Hi Bhavesh/Kanwaljit,
If we have multiple ACCESS tags then what happens is that the connection to the database is made only once. But the data is inserted row by row.
Why i am saying this?
If we add the following in JDBC Adapter..logSQLStatement = true. Then incase of multiple inserts we can see that there are multiple
<i>Insert into tablename(EMP_NAME,EMP_ID) VALUES('J','1000')
Insert into tablename(EMP_NAME,EMP_ID) VALUES('J','2000')</i>
Doesnt this mean that rows are inserted one by one?
Correct me if i am wrong.
This does not mean that the transaction is not guaranted. Either all the rows will be inserted or rolled back.
Regards,
Sumit
Maybe you are looking for
-
My Ipod has reseted and i lost all my music how can i get it back?
I have somehow made my ipod reset and i lost all of my music how can i get it back on my ipod? I need help.?!?!?!?? and calling the 1-800 number didnt help.
-
How can I get my serial number for CS 5.5 Master Collection and CS 5.5 Captivate? This was retrievable only a few days ago using the "My Adobe" site at (adobe.com/account.html) The My Adobe URL (adobe.com/account.html) has a permanent redirect to the
-
Can I change my payment date with creative cloud
-
Everything prints in landscape
Suddenly all my documents print in landscape mode. This is true for all programs and all printers (laser, inkjet). Clicking on portrait mode and/or landscape mode in 'page setup' has no effect. It looks fine in page preview mode Any suggestions? Than
-
11g Tp3 Tutorial execution problems
I tried creating the 1st JDeveloper targeted Toplink tutorial at http://www.oracle.com/technology/products/ias/toplink/doc/11110/tutorial/index.htm. What are the correct libraries to execute the test? I have TopLink Oracle XML Parser v2 Oracle JDBC *