Teradata does it have a ROLLBACK - teradata

BEGIN TRANSACTION;
DELETE FROM DW_RX_V.RX_FLL_FCT
WHERE rx_nbr = IN_RX_NBR
AND rx_fll_dte = CAST(IN_RX_FLL_DTE AS TIMESTAMP(6) FORMAT 'MMDDYYYY')
AND rfl_tie_brk_nbr = IN_RFL_TIE_BKR;
IF :ACTIVITY_COUNT = 0
THEN
ROLLBACK;
SIGNAL ERROR_HANDLER;
END IF;
DELETE from DW_RX_V.RX_FLL_DET
WHERE rx_nbr = IN_RX_NBR
AND rx_fll_dte = CAST(IN_RX_FLL_DTE AS TIMESTAMP(6) FORMAT 'MMDDYYYY')
AND rfl_tie_brk_nbr = IN_RFL_TIE_BKR;
IF :ACTIVITY_COUNT = 0
THEN
ROLLBACK;
SIGNAL ERROR_HANDLER;
END IF;
END TRANSACTION;
I just tried using the stored procedure block above, upon running, it still not able to rollback the transaction upon failure.
Does Teradata really have a rollback?
So what I want is, everytime there is no record found on the second DML statement, then rollback everything what is deleted from the start.

Related

Can anyone help whey my execption section is not working,

My whole intention to catch exception,WRONG parameter is NOT CATCHING exception.
Here is the code:
CREATE OR REPLACE PROCEDURE list_emp (p_emp_id IN employees.employee_id%TYPE,
p_dept_id IN employees.department_id%TYPE)
IS
CURSOR c1 IS
SELECT *
FROM EMPLOYEES
WHERE EMPLOYEE_ID=p_emp_id
AND DEPARTMENT_ID=p_dept_id;
emp_rec c1%ROWTYPE;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO emp_rec;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_rec.employee_id||' '||emp_rec.first_name||' '||emp_rec.last_name);
END LOOP;
CLOSE c1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No Record Found ');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('No Record Found ');
END;
When the cursor is opened and fetched with the wrong parameter that does not match any row from the corresponding table, the following line
EXIT WHEN c1%NOTFOUND;
cause the plsql procedure to terminate (because there were no rows found). Hence no exception is raised.
If you do want to display some sort of output you can do the following instead
IF c1%FOUND THEN
dbms_output.put_line('Record Found');
ELSE
dbms_output.put_line('Finished/Done');
EXIT;
END IF;
If you want to raise an error after looping through a cursor that returns no rows, then you're going to have to use a counter to work out how many rows have been processed, and then you can do something if no rows have been processed.
Something like:
create or replace procedure list_emp (p_emp_id in employees.employee_id%type,
p_dept_id in employees.department_id%type)
is
cursor c1 is
select employee_id,
first_name,
last_name
from employees
where employee_id = p_emp_id
and department_id = p_dept_id;
v_count number := 0;
begin
for emp_rec in c1
loop
v_count := v_count + 1;
dbms_output.put_line(emp_rec.employee_id||' '||emp_rec.first_name||' '||emp_rec.last_name);
end loop;
if v_count = 0 then
raise no_data_found;
end if;
exception
when no_data_found then
dbms_output.put_line('No Record Found.');
raise;
when others then
dbms_output.put_line('An error occurred: '||sqlerrm);
raise;
end;
/
A few notes:
I converted your cursor loop into a cursor-for-loop; you don't need to worry about declaring the record type and also Oracle handles the opening and closing of the cursor for you.
I added raise; to each of your exception handlers - in general, having when others then null (which is effectively what your original code was doing - no errors are raised to the calling code) is a bad idea. I added the raise to the no_data_found condition as that wasn't doing anything either; typically, if you have an exception condition, you want it to do something to let the calling code know there was a problem (not always, of course; sometimes you don't want the processing to stop if a particular error condition is met).
Your cursor was selecting all columns, but in your procedure, you were only using three of them. I've therefore amended the cursor so that it only pulls back those three columns.
Don't rely on dbms_output in your production code. Code that calls this procedure won't see anything populated in dbms_output, unless it explicitly looks for it - and that's not something I've ever seen in any production code, outside of Database tools (eg. SQL*Plus, Toad, etc). I've left this in your procedure as I've a feeling this is a learning exercise for you, but please don't think that this is in any way acceptable in production code.
You're passing p_emp_id in as a parameter - typically, that's the primary key of the employees table. If that's the case, then there's no need for the cursor for loop at all - you could do it by using select ... into ... instead, like so:
.
create or replace procedure list_emp (p_emp_id in employees.employee_id%type,
p_dept_id in employees.department_id%type)
is
v_emp_id employees.employee_id%type;
v_first_name employees.first_name%type;
v_last_name employees.last_name%type;
begin
select employee_id,
first_name,
last_name
into v_emp_id,
v_first_name,
v_last_name
from employees
where employee_id = p_emp_id
and department_id = p_dept_id;
dbms_output.put_line(emp_rec.employee_id||' '||emp_rec.first_name||' '||emp_rec.last_name);
exception
when no_data_found then
dbms_output.put_line('No Record Found.');
raise;
when others then
dbms_output.put_line('An error occurred: '||sqlerrm);
raise;
end;
/
Alternatively, just pass back a ref cursor:
create or replace procedure list_emp (p_emp_id in employees.employee_id%type,
p_dept_id in employees.department_id%type,
p_ref_cur out sys_refcursor)
is
begin
open p_ref_cur for select employee_id,
first_name,
last_name
from employees
where employee_id = p_emp_id
and department_id = p_dept_id;
-- No need for an exception handler here since you're not storing the error details anyway.
-- By not having an error handler, any error will automatically be raised up to the calling code
-- and it will have the correct error stack trace info (e.g. the line number the error occurred,
-- rather than the line the error was reraised from
end;
/
And to run the ref cursor in SQL*Plus (or as a script in Toad/SQL Developer/etc), you do the following:
-- create a variable outside of PL/SQL to hold the ref cursor pointer (this is a SQL*Plus command):
variable rc refcursor;
-- populate our ref cursor variable with the pointer. Note how we pass it in as a bind variable
begin
list_emp(p_emp_id => 1234,
p_dept_id => 10,
p_ref_cur => :rc);
end;
/
-- finally, print the contents of the ref cursor.
print rc;

Table is mutating, trigger/function may not see it

I create a trigger to check COUNT.
create or replace
TRIGGER TEST_TRG before INSERT OR UPDATE ON TEST
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
DECLARE
AVAILABLE INTEGER;
BEGIN
IF UPDATING THEN
IF(:new.STATUS = 600 OR :new.STATUS = 700) THEN
SELECT COUNT(1) INTO AVAILABLE FROM TEST T WHERE T.IDDISPLAY = :new.IDDISPLAY
AND T.STATUS NOT IN (600,700);
IF(AVAILABLE = 0) THEN
InsertOrUpdateAnotherTable(:new.IDDISPLAY, :new.STATUS, 0);
ELSE
RETURN;
END IF;
END IF;
END IF;
After I change the status to 600 in Test Table, there are errors.
ORA-04091: table USER.TEST is mutating, trigger/function may not see it
ORA-06512: at "USER.TEST_TRG ", line 8
ORA-04088: error during execution of trigger 'USER.TEST_TRG'
I will insert or update another table if the condition is met.
The error is because I try to get COUNT of the current table when the trigger is triggering the same table. This will cause mutating happens.
I have try transaction_anonymous and Compound Trigger, but at the end still same error occurs.
Anyone can help me for another solution, please.
Firstly:
This below line
IF(AVAILABLE = 0) THEN
never will be reached, even if you could eliminate mutating error.
Checking count like this: COUNT(1) INTO AVAILABLE will throw exception "no data found" when there is really no records you're quering.
Secondly:
using compund trigger with section AFTER EACH ROW let you avoid mutating as Tom wrote http://www.dba-oracle.com/t_avoiding_mutating_table_error.htm:
Try this:
create or replace
TRIGGER TEST_TRG FOR INSERT OR UPDATE ON TEST
COMPOUND TRIGGER
AVAILABLE INTEGER;
AFTER EACH ROW IS
BEGIN
IF UPDATING THEN
IF(:new.STATUS = 600 OR :new.STATUS = 700) THEN
BEGIN
SELECT COUNT(1) INTO AVAILABLE FROM TEST T WHERE T.IDDISPLAY = :new.IDDISPLAY
AND T.STATUS NOT IN (600,700);
EXCEPTION
WHEN NO_DATA_FOUND THEN
InsertOrUpdateAnotherTable(:new.IDDISPLAY, :new.STATUS, 0);
END;
END IF;
END IF;
END AFTER EACH ROW;
END TEST_TRG;

ORA-00905: missing keyword in SQL PLus

I am getting an ORA-00905: missing keyword error when running in the following procedure using the SQLPLUS command line. Strangely it compiles and works when I run it in via an SQL window in PL/SQL developer, unfortunately I need it to work via the command line as well:
CREATE OR REPLACE PROCEDURE PRO_INSERT_ALERT_END_DATE IS
CURSOR cur_InsertEndDate IS
SELECT cli_per_id,
date_ended,
date_started,
alertid
FROM CP_END_ALERT;
BEGIN
FOR rec_cur_InsertEndDate IN cur_InsertEndDate
LOOP
BEGIN
UPDATE vwe_alert_table
SET alert_inactive_on = rec_cur_InsertEndDate.date_ended,
alert_inac_reason = 'Deregistered'
WHERE vwe_alert_table.art_id = rec_cur_InsertEndDate.alertid AND
vwe_alert_table.art_per_id = rec_cur_InsertEndDate.cli_per_id AND
vwe_alert_table.art_alerted_on = rec_cur_InsertEndDate.date_started AND
vwe_alert_table.art_alert = 'AL02';
COMMIT;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error updating record ' || SUBSTR(SQLERRM, 1, 250));
ROLLBACK;
END;
END LOOP;
END PRO_INSERT_ALERT_END_DATE;
Any advice would be most welcome
It is probably due to the blank lines in the script. Tell SqlPlus to ignore them
set sqlblanklines on

SQL Server stored procedure returning 'fail'

I have a stored procedure in SQL Server 2008 that needs to delete a few rows of data. But when I run it, it returns a fail and a value of -6.
ALTER procedure [dbo].[p_CaseFiles_Exhibits_DeleteExhibits]
#ExhibitID int
, #Message nvarchar(50) output
as
declare #FileID int
set #FileID = (select FileID from CaseFileExhibits where ExhibitID = #ExhibitID)
begin transaction
begin try
delete from CaseFileExhibitMovementTracking where ExhibitID = #ExhibitID
delete from CaseFileExhibitAttachments where CaseFileExhibitID = #ExhibitID
delete from CaseFileExhibits where ExhibitID = #ExhibitID
delete from CaseFileExhibitPropertyLink where ExhibitID = #ExhibitID
update CaseFileQuickStats set ExhibitCount = ExhibitCount -1 where CaseFileID = #FileID
commit transaction
end try
begin catch
set #Message='Fail'
rollback transaction
end catch
I can't seem to find what's wrong.
You're able to check out the messages yourself, add this to your CATCH block:
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
You may want to change that SELECT to PRINT, and then you'll be able to see the results in the 'Messages' tab when running the SP within SSMS.
I suspect it's a problem with a Foreign Key or a possible trigger.

.Net Oracle ORA-24338 'statement handle not executed' error and some error in one stored Procedure

I have the following Stored Procedures
create or replace PROCEDURE WEB_AC
(
v_iDocid IN NUMBER DEFAULT NULL ,
v_valor IN VARCHAR2 DEFAULT NULL ,
v_campo IN VARCHAR2 DEFAULT NULL ,
v_error OUT NUMBER
)
AS
v_campoid NUMBER(5,0);
v_tipodato VARCHAR2(50);
v_DOCTYPE NUMBER;
v_tabla VARCHAR2(50);
v_procedure VARCHAR2(70);
BEGIN
v_error:= 0;
IF v_valor IS NULL
OR v_valor IS NULL
OR LENGTH(TRIM(v_valor)) = 0 THEN
BEGIN
v_error:= 3;
END;
else
Begin
bEGIN
SELECT campoid,
doctype,
tipodato
INTO v_campoid,
v_DOCTYPE,
v_tipodato
FROM TiposDocumento t
, DIGITALIZAMAIN d
, CatCamposDocumento c
where
c.tabla=t.tabla and
nombre=v_campo and
doctype = TipoDocumentoID and
docid = v_iDocid AND
Mostrar = 1;
EXCEPTION
WHEN OTHERS THEN
v_campoid := 0;
END;
--select #campoid
IF v_campoid != 0 THEN
Begin
EXECUTE IMMEDIATE 'BEGIN ABANCE3.WEB_UPDOC' || TRIM(TO_CHAR(v_DOCTYPE )) || 'C' || TRIM(TO_CHAR(v_campoid)) ||'(' ||
TO_CHAR (v_iDocid)||' , '||CHR(39)||v_valor||CHR(39)||',:2);END;'
USING out v_error;
END;
END IF;
end;
end if;
END;
And
create or replace PROCEDURE WEB_UPDOC1C6(v_idreg NUMBER,v_valor VARCHAR2,v_temp OUT NUMBER)
AS
v_sys_error NUMBER := 0;
BEGIN
BEGIN
SELECT count(*)
INTO v_sys_error
FROM DOC1
where DOCID = v_idreg;
EXCEPTION WHEN OTHERS THEN v_sys_error:=0;
END;
IF v_sys_error > 0 THEN
BEGIN
BEGIN
UPDATE DOC1
SET DESCRIPCION = v_valor
WHERE DOCID = v_idreg;
EXCEPTION WHEN OTHERS THEN v_sys_error:=0;
END;
IF v_sys_error = 0 THEN v_temp:=0 ;
ELSE v_temp:=1 ;
END IF;
END;
END IF;
END;
and I'm calling them from an Application with this code:
Friend Function ActualizaCampos(ByVal iDocID As Long, ByVal valor As String, ByVal Campo As String, ByVal ProyectoID As Integer) As String
Dim mstrCS as String = "Here goes the connection String to my server"
Dim db As Database
Dim dbCW As DbCommand
Dim iValor As String = "0"
Select Case Me.TipoBD
Case GlobalDef.eTipoBD.Oracle
db = New OracleDatabase(mstrCS)
dbCW = db.GetStoredProcCommand(WEB_AC, iDocid, valor, Campo, 0)
db.ExecuteNonQuery(dbCW)
Case GlobalDef.eTipoBD.SQLServer
db = New SqlDatabase(mstrCS)
dbCW = db.GetStoredProcCommand(WEB_AC, iDocid, valor, Campo)
iValor = db.ExecuteScalar(dbCW).ToString()
End Select
Return iValor
End Function
In this example the WEB_AC SP always execute the sp WEB_UPDOC1C6
I have two problems with this.
First problem: At some point in the application I have the valor parameter (of the visual Basic Function) as a string with spaces that is something like "some string with spaces". When this happens, the stored procedure don't update the table. If I execute the SP directly in the DB (with SQL Developer) all works fine. I know it has something to do with the string missing some quotes(') but I haven't make it work yet. Some ideas on this?
Second problem: Sometimes, when debuging the application, if I interrupt the execution, I start getting the ORA-24338 'statement handle not executed' error for hours every time I try to execute it again. I believe it has something to do with an open transaction. But honestly, as I'm new in working with Oracle, I really have no idea what the problem could be.
Can you help me?
UPDATE: I have found the ORA-24338 real reason. It was another SP that was causing the error. When i found the solution to my other problem I'll post it all here.
I'd suggest that you replace your WHEN OTHERS clause with one that specifically names the erros that you are expecting, or you raise the error after handling it anyway. WHEN OTHERS is a bit contraversial as it is notorious for hiding the real problem.

Resources