How to trigger automatic update upon data insertion? - triggers

I am using SSIS 2008 and am trying to update one column in my table at the time of insertion. This one column is a uniqueidentifier field and I also wrote a trigger for updating this field. This code is below:
CREATE TABLE dbo.rd_information3_cleaned (
c1 uniqueidentifier NULL,
c2 nvarchar(50),
c3 nvarchar(50),
c4 nvarchar(50)
)
create trigger dbo.trg_client_information_id
on client_information
after insert
as
begin
update client_information
set client_information_id = newid()
from Inserted
end
I know this code works cause I tested it in SSMS and it does update this column. Also, my table looks like:
c1 c2 c3 c4
xxxx-xxxx-xxxx-xxxx A BB C5
xxxx-xxxx-xxxx-xxxx A2 BB C
xxxx-xxxx-xxxx-xxxx A3 BB C7
xxxx-xxxx-xxxx-xxxx A4 BB C
But when I try to run this SSIS package, I only write to c2 - c4, since the trigger should update column "c1". But instead, I am getting an error:
Information: 0x40043007 at Write to Client_Information, SSIS.Pipeline: Pre-Execute phase is beginning.
Information: 0x4004300C at Write to Client_Information, SSIS.Pipeline: Execute phase is beginning.
Error: 0xC0202009 at Write to Client_Information, Client_Information [27]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "The statement has been terminated.".
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "Cannot insert duplicate key row in object 'dbo.Client_Information' with unique index 'IX_Client_Demographics_Unique'.".
Error: 0xC0209029 at Write to Client_Information, Client_Information [27]: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "input "OLE DB Destination Input" (40)" failed because error code 0xC020907B occurred, and the error row disposition on "input "OLE DB Destination Input" (40)" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure.
Error: 0xC0047022 at Write to Client_Information, SSIS.Pipeline: SSIS Error Code DTS_E_PROCESSINPUTFAILED. The ProcessInput method on component "Client_Information" (27) failed with error code 0xC0209029 while processing input "OLE DB Destination Input" (40). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running. There may be error messages posted before this with more information about the failure.
Information: 0x40043008 at Write to Client_Information, SSIS.Pipeline: Post Execute phase is beginning.
Information: 0x402090DF at Write to Client_Information, Client_Information [27]: The final commit for the data insertion in "component "Client_Information" (27)" has started.
Information: 0x402090E0 at Write to Client_Information, Client_Information [27]: The final commit for the data insertion in "component "Client_Information" (27)" has ended.
Information: 0x4004300B at Write to Client_Information, SSIS.Pipeline: "component "Client_Information" (27)" wrote 2 rows.
Information: 0x40043009 at Write to Client_Information, SSIS.Pipeline: Cleanup phase is beginning.
Task failed: Write to Client_Information
SSIS package "Echo Information Migration2.dtsx" finished: Success.
The program '[2564] Echo Information Migration2.dtsx: DTS' has exited with code 0 (0x0).
I am almost sure the cause of this error is that client_information_id field. cause I am able to write more than one row in SSMS if I just make that field uniqueidentifier; otherwise I can't write more than one row to this table. So I am wondering. Is it possible that I need to set a TIME property in SSIS to give the trigger enough time to work? Why else could I be getting this error?
Also, I edited the OLE DB Destination and I set Maximum insert commit size = 1, but I still got the same error.

Long story short, don't use a trigger, just use a DEFAULT as shown in the documentation. The table DDL and trigger code you posted seem to have nothing to do with each other, but I assume you really want something like this:
create table dbo.Clients (
ClientID uniqueidentifier not null primary key default newid(),
ClientAttributeA nvarchar(50) not null,
-- etc.
)
I suspect that when you tested in SSMS you tested inserting one row at a time, but your SSIS package is inserting multiple rows. The NEWID() function is called only once in the trigger, so if you insert one row it will work but if you insert multiple rows then you get same NEWID() value for each one, causing your duplicate key violation.

Related

Query returns Error despite being executed succesfully (Robot Framework / JayDeBeApi)

Using the Keyword Query from the Robot Framework DatabaseLibrary JayDeBeApi in conjunction with DB2 like this: ${results}= Query CREATE TABLE SCHEMANAME.TEST_TEMP (id BIGINT, name VARCHAR(25)) is being executed (table exists afterwards).
But nevertheless RobotFramework throws a FAIL and ${results} contains the Message DatabaseError: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-601, SQLSTATE=42710, SQLERRMC=SCHEMANAME.TEST_TEMP;TABLE, DRIVER=4.14.122 and often even a very simple Message Error after running the same statement.
Running the query above (copy/paste) directly within a database SQL window doesn't return any errors.
How is it possible, in RobotFramework the query is executed successfully but nevertheless an error is thrown?
The error SQLCODE=-601 means that you are trying to create an object that already exists. So when you say that the table exists afterwards, it means that it existed before you ran the statement. I don't know the framework you are using, but the explanation by #pavelsaman in comment seems to be a very likely cause.

How to create a trigger before insert in db2

I have three tables below:
I need to create a trigger to disallow students to take a class without completing their pre-requisites (must use a trigger). The trigger must return an error message "Missing Pre-req" when trying to insert a class without its proper pre-req.
So far, I wrote a command below:
create trigger checkPrereq
before insert on HW3.SCHEDULE
referencing new as newRow
for each row
When
(
select GRADE
from HW3.SCHEDULE
where HW3.SCHEDULE.ClassId =
(
select PrereqId from HW3.CLASS_PREREQ
where newRow.ClassId = HW3.CLASS_PREREQ.ClassId
)
) in ('F', null)
begin atomic
set newRow.Semester = null;
signal sqlstate 'KILLED' set message_text = ('Missing Pre-req');
end
but I got a warning:
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0104N An unexpected token "GRADE" was found following
"ach row When (select". Expected tokens may include: ")". LINE
NUMBER=1. SQLSTATE=42601
I can not understand what was happening here. Could you please help fix this? Any help is appreciated!

IBM DB2 error sql code-724 & sql state 54038

I was trying to insert some sample data to the table but the db2 command line processor return this message "DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0723N An error occurred in a triggered SQL statement in trigger
"EDWIN.CALLCQ". Information returned for the error includes SQLCODE "-724",
SQLSTATE "54038" and message tokens "EDWIN.CHKQUANTITY|PROCEDURE ".
SQLSTATE=09000"
Here is my procedure
create procedure chkQuantity (Cart_ID int,Food_ID int, Food_Quantity int)begin declare c cursor with return for select sum(Food_Quantity) from Cart_details group by Cart_ID;open c; If(Food_Quantity <= 10)then insert into cart_details(Cart_ID,Food_ID,Food_Quantity) values (Cart_ID , Food_ID ,Food_Quantity);Else signal sqlstate'45000' set message_text = '1 Cart Maximum order only 10 food Quantity' ;delete from cart_details where cart_details_id=cart_details_id; end if;close c; end
trigger
create trigger callCQ after insert on cart_details referencing new as N for each row mode db2sql call chkQuantity(N.Cart_ID, N.Food_ID, N.Food_Quantity)
table
create table Cart_Details(Cart_Details_ID int not null primary key ,Cart_ID int , Foreign Key(Cart_ID) references Cart,Food_ID int,foreign key(Food_ID) references Food, Food_quantity int check(food_quantity <= 10))
The description of the SQL0724N message clearly states that:
SQL0724N
The activation of object-name of type object-type would
exceed the maximum level of indirect SQL cascading.
Explanation
Cascading of indirect SQL occurs when a trigger activates another
trigger (possibly through referential constraint delete rules) or a
routine, containing SQL, invokes another routine. The depth of this
cascading is limited to 16 for triggers and 64 for routines. Note that
recursive situations where a trigger includes a triggered SQL
statement that directly or indirectly causes the same trigger to be
activated, or where a routine directly or indirectly invokes itself,
is a form of cascading that is very likely to cause this error if
there are no conditions to prevent cascading from exceeding the limit.
The object-type is one of TRIGGER, FUNCTION, METHOD, or PROCEDURE. The
object-name specified is one of the objects that would have been
activated at the seventeenth level of cascading.
User response
Start with the objects that are activated or invoked by the statement that
received this error. If any of these objects are recursive, ensure
that there is some condition that prevents the object from being
activated or invoked more than the limit allows. If this is not the
cause of the problem, follow the chain of objects that are activated
or invoked to determine the chain that exceeds the cascading limit.
You call a routine inserting a row into a table in the after insert trigger. This leads to recursive calls of this trigger exceeding the allowed number of cascading calls.

Getting SQL state: 25P02 while trying to update a table with data from another

I am using a PostgreSQL DB. I have two tables, namely company and bl_location. I have a field location_id in both tables. Now I am trying to copy the value of location_id from bl_location to company. The primary key of company is company_id and it is stored in bl_location too. I am trying the following query:
UPDATE company
SET location_id = bl_location.location_id
from bl_location
where company.company_id = bl_location.company_id;
using the syntax I found online:
update table1
set col1 = . . .
from table2
where table1.id = table2.table1_id
But I am getting the following in the console of the pgAdmin tool:
ERROR: current transaction is aborted, commands ignored until end of transaction block
********** Error **********
ERROR: current transaction is aborted, commands ignored until end of transaction block
SQL state: 25P02
I cannot figure out what is wrong with my query.
RCA : This issue happens when last transitions ended up with error and not rolled back
FIX : psql > ROLLBACK
if you're using PGAdmin click on rollback & enable auto rollback option
Whether you are aware of it or not, your statement is running inside an explicit transaction, and one of the preceeding statements is the same transaction must have caused an error.
To debug this, consider setting log_statement = 'all' so that all statments get logged. By tracing all statements from the same backend PID you will be able to identify the culprit.

Table Valued User Defined Functions in DB2 Z/OS

Does anyone know if DB2 v9.1 z/OS supports Table Valued User Defined Functions?
This is what I am trying to create but I keep getting the error message below.
CREATE FUNCTION func_test(v_vchCol CHAR(10))
RETURNS TABLE(col_a char(10), row_cnt integer)
LANGUAGE SQL
SPECIFIC FUNCINFO
NOT DETERMINISTIC
READS SQL DATA
return
select col_1, count(*)
from SCHEMA_NAME.TEST1
where col_1 = v_vchCol
group by col_1;
Error Message:
ERROR [56038] [IBM][DB2] SQL0969N There is no message text
corresponding to SQL error "-4700" in the message file on this
workstation. The error was returned from module "DSNHSMS1" with
original tokens "". SQLSTATE=56038
Any help would be much appreciated
Yes, but it appears to require new function mode which apparently isn't enabled yet in the DB2 instance you're connected to.