Hey guys this is a stored procedure that updates the PrevLoc field..but I want my proc to insert the upadate in to different field I do not want this to modify the original field as I still want to keep it as it is.Could you guys help on this?I greatly appreciate it.Thanks all
ALTER PROC [dbo].[updateloc]
AS
UPDATE Stage.Loc
SET PrevLoc=RTRIM(PrevLoc)
UPDATE Stage.Loc
SET PrevLoc = REPLACE(PrevLoc, substring(PrevLoc, LEN(PrevLoc) -
(CHARINDEX(' ', REVERSE(PrevLoc)))+ 1, LEN(PrevLoc)), dbo.parsLocat(PrevLoc))-- --this is a function that I use
SET changedLoc=PrevLoc---this doesnt work
GO
is changeLoc a column in the table?
You would just need to set that property instead.
These changes would leave PrevLoc with no changes at all, and process that value into changeLoc
ALTER PROC [dbo].[updateloc]
AS
UPDATE Stage.Loc
SET changeLoc=RTRIM(PrevLoc)
UPDATE Stage.Loc
SET changeLoc = REPLACE(changeLoc, substring(changeLoc, LEN(changeLoc) -
(CHARINDEX(' ', REVERSE(changeLoc)))+ 1, LEN(changeLoc)), dbo.parsLocat(changeLoc))-- --this is a function that I use
GO
Related
Need help on getting query on a text file using stored procedure in db2. I'm very new to this so please help.
Till now i am able to create a file and get get data in created file. But i want a complete query in the file not a single column data.
I have created a Stored Procedure like this
CREATE or replace PROCEDURE SCOPEMASTERP(my_statement varchar(10000))
AUTONOMOUS
BEGIN
DECLARE v_filehandle UTL_FILE.FILE_TYPE;
DECLARE v_dirAlias VARCHAR(50) DEFAULT 'mydir';
DECLARE v_filename VARCHAR(100) DEFAULT 'bluestar_transaction-';
DECLARE v_format VARCHAR(200);
SET v_format = '%s\n';
set v_filename = concat(v_filename, VARCHAR_FORMAT(current_date, 'DD-MM-YYYY'));
set v_filename = concat(v_filename, '.log');
CALL UTL_DIR.CREATE_OR_REPLACE_DIRECTORY('D:', '/archivelog/asd/');
SET v_filehandle = UTL_FILE.FOPEN(v_dirAlias,v_filename,'a');
CALL UTL_FILE.PUTF(v_filehandle,v_format, my_statement);
CALL DBMS_OUTPUT.PUT_LINE('Wrote to file: ' || v_filename);
CALL UTL_FILE.FCLOSE(v_filehandle);
END
and i have created a trigger inside trigger i am calling stored procedure
CREATE OR REPLACE TRIGGER SCOPEMASTER_Trigger
AFTER INSERT ON SERVEIT.SCOPENAMEMASTER
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
call SCOPEMASTERP(N.SCOPENAMEID);
END
insert statement i am executing
Insert into SERVEIT.SCOPENAMEMASTER (SCOPENAMEID) values (1013)
GO
And file which is creating in "D" drive i am getting
But instead of just getting 1013 i need the complete query in the file
Insert into SERVEIT.SCOPENAMEMASTER (SCOPENAMEID) values (1013)
What changes i need to do please help. Thanks in advance!!
There are no special registers/variables/etc. available in DB2 that provide the SQL statement that is being executed, so what you're asking for is not possible.
I'm looking for help with a stored procedure in teradata. I want to update a whole table and for this I'm trying to use a for loop cursor. the problem is that my update is defined via column names passing through parameters to the SP.
I've seen it can be possible to use dynamic sql to do that but I haven't found any information on the subject concerning for loop cursor and dynamic sql. Is it possible with FOR LOOP CURSOR ?
I've tried to do only the select and calculs with dynamic sql, it works fine but then the problem is to update the table from the cursor on the select. In this case how to update a table from my cursor?
I let you show my code.
loop cursor :
REPLACE PROCEDURE [database].calDELAI
(
IN dateDebut VARCHAR(30),
IN dateFin VARCHAR(30),
IN delay VARCHAR(30)
)
BEGIN
DECLARE DATE_DEBUT_COLONNE VARCHAR(64);
DECLARE DATE_FIN_COLONNE VARCHAR(64);
SET DATE_DEBUT_COLONNE=dateDebut;
SET DATE_FIN_COLONNE=dateFin;
FOR for_loop_update AS cur_select_set CURSOR FOR
SELECT
TMP.DATE_FIN_COLONNE-TMP.DATE_DEBUT_COLONNE
FROM [database].ORD_T_DETL_ORDR_DELAI AS TMP
/* the select is more complicated but here is the spirit of it.*/
DO
IF (delay='DELAI1') THEN SET DELAI1=NB_JR_OUVRABLE;
END IF;
END FOR ;
END ;
The errors given by teradata are :
SPL1027:E, Missing/Invalid SQL statement'E(3810):Column/Parameter '[database].TMP.DATE_FIN_COLONNE' does not exist.'.
SPL2001:E, Undefined symbol 'DELAI1'.
SPL2001:E, Undefined symbol 'NB_JR_OUVRABLE'.
Thanks in advance for your replies and your help.
The call statement should contain all the input Paramenters , make sure you are specifying all the input parameters correctly. Could you please provide your call statement.
I am currently creating an application but when running the following code:
ADOQRanking.Edit;
ADOQRanking['Bye']:=True;
ADOQRanking['TableAssigned']:=True;
ADOQRanking.Post;
... a new record with null values is created in ADOTPoints (the table that ADOQRanking sorts), with Bye and TableAssigned both set to true, rather than the intended record being edited. It should also be noted that there is no code in the entire procedure that creates a record in ADOTPoints.
try to use fields by it's names
ADOQRanking.FieldByName('Byte').Value := true ;
and so on ..
I have an enum type with default value as the following:
app_type apps_status NOT NULL DEFAULT 'undeployed'::apps_status
But now whenever I try to insert I get an error app_type cannot be empty. Also when you continue trying you will get error: Number has already been taken.
Any idea why this is happening?
The issue is the NOT NULL constraint, which is filled by the ORM, and not deferrable.
First off, try setting the value to DEFAULT on the ORM side.
If it's not a workable solution in your ORM, try adding a trigger that sets the default value:
create function make_default() returns trigger as $$
begin
new.app_status := 'undeployed'::apps_status
return null;
end;
$$ language plpgsql;
create trigger before insert or delete on yourtable
for each row when (new.app_type is null)
execute procedure make_default();
If that doesn't work either, drop the NOT NULL constraint altogether, and enforce it with a constraint trigger instead:
create function reject_row() returns trigger as $$
begin
raise exception 'rejected!'
return null;
end;
$$ language plpgsql;
create constraint trigger after insert or delete on yourtable
for each row when (new.app_type is null)
execute procedure reject_row();
Thanks to everyone who helped. However after thorough research I realized that PostgreSQL ENUM doesn't support default but domains do so I use domain instead. Domain is working fine and is inserting default value as well.
See the following link:
http://www.sqlines.com/postgresql/how-to/create_user_defined_type
I am trying to create a BEFORE INSERT trigger that will check the incoming value of a field, and replace it with the same field in another row if that the field is null. However, when I add the CALL statement to my trigger, an error is returned "The trigger "ORGSTRUCT.CSTCNTR_IN" is defined with an unsupported triggered SQL statement". I checked the documentation and saw that cursors weren't supported in the BEFORE (part of the reason for making the stored procedure in the first place), but even when I remove the cursor declaration from the stored procedure the call still generates the same error.
Trigger:
CREATE TRIGGER orgstruct.cstcntr_IN
NO CASCADE
BEFORE INSERT ON orgstruct.tOrgs
REFERENCING NEW AS r
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE prnt_temp BIGINT;
DECLARE cstcntr_temp CHAR(11);
SET prnt_temp = r.prnt;
SET cstcntr_temp = r.cstcntr;
CALL orgstruct.trspGetPrntCstCntr(prnt_temp,cstcntr_temp);
SET r.cstcntr = cstcntr_temp;
END
Stored procedure:
CREATE PROCEDURE orgstruct.trspGetPrntCstCntr (
IN p_prnt BIGINT,
OUT p_cstcntr CHAR(11)
)
SPECIFIC trGetPrntCstCntr
BEGIN
IF p_prnt IS NULL THEN
RETURN;
END IF;
BEGIN
DECLARE c1 CURSOR
FOR
SELECT cstcntr
FROM orgstruct.tOrgs
WHERE id = p_prnt
FOR READ ONLY;
OPEN c1;
FETCH FROM c1 INTO p_cstcntr;
CLOSE c1;
END;
END
According to the documentation, CALL is allowed in a BEFORE trigger, so I don't understand what the problem is.
A before trigger can call a stored procedure, but the stored proc can't do anything not allowed in the trigger.
In your case, the default level of data access for a SQL stored proc is MODIFIES SQL DATA, which is not allowed in the trigger. You could recreate your stored procedure, changing the data access level to READS SQL DATA; this will allow you to create the trigger.
However: There is no reason to call a stored procedure for something this simple; You can do it using a simple inline trigger:
create trigger orgstruct.cstcntr_IN
no cascade
before insert on orgstruct.tOrgs
referencing new as r
for each row
mode db2sql
set r.cstcntr = case
when r.p_prnt is not null
then (select cstcntr from tOrgs where id = r.p_prnt fetch first 1 row only)
else r.cstcntr
end;
This will be a LOT more efficient because it eliminates both the stored procedure call and the cursor processing inside the stored proc. Even if you wanted to use the stored proc, you could eliminate the cursor inside the stored proc and improve performance.
FYI: the logic that you posted contains an error, and will always set CSTCNTR to NULL. The trigger posted in this answer not do this. :-)