473,462 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inserting old value into a column

I have a table cc_rd_user_questions with the following columns:

NUM NUMBER (25) DEFAULT 1 NOT NULL,
COMPANY_ID NUMBER (15) NOT NULL,
PROJ_ID NUMBER (15) NOT NULL,
SEQ NUMBER (25) NOT NULL,
QUEST_ID NUMBER (15) NOT NULL,
RESPONSE VARCHAR2 (40),
CREATE_DATE DATE NOT NULL,
LAST_UPDATE DATE NOT NULL

This table contains the responses to questions found on a form. The
SEQ column is incremented each time a new row is inserted for a
specific company and specific project). If during an insert the new
RESPONSE column is null, what is the best method to populate the
RESPONSE column with the previous RESPONSE column value (the previous
row where COMPANY_ID, PROJ_ID and QUEST_ID are the same but SEQ = SEQ
- 1)? Should I use a trigger (I forsee a "mutating" table problem), a
temp table or SQL?

Thanks in advance!

- Rey
Jul 19 '05 #1
4 5322
You can build your SQL so that it doesn't pass in the Response column if it
is null. I think this is the best solution. However, it is also possible
to use a trigger. It's a simple solution, and easy to implement, but it's
not as efficient or elegant as making sure your app just doesn't update the
Reponse column if it is null on the screen. Having said that, you don't
need to run into the mutating table problem, just use the :OLD and :NEW
aliases to set the value. For example:

SQL> create table cc_rd_user_questions (
2 num number(25) default 1 not null,
3 company_id number(15) not null,
4 seq number(25) not null,
5 quest_id number(15) not null,
6 response varchar2(40),
7 create_date date default sysdate not null,
8 last_update date default sysdate not null);

Table created.

SQL> list
1 create or replace trigger cc_rd_user_response_fixup_trig
2 before update
3 on cc_rd_user_questions
4 for each row
5 begin
6 if :new.response is null then
7 :new.response := :old.response;
8 end if;
9* end;
SQL> /

Trigger created.

SQL> insert into cc_rd_user_questions values (1, 2, 3, 4, 'Response One',
sysdate, sysdate);

1 row created.

SQL> select Response from cc_rd_user_questions where num = 1;

RESPONSE
----------------------------------------
Response One

SQL> update cc_rd_user_questions set Response=NULL where NUM=1;

1 row updated.

SQL> select Response from cc_rd_user_questions where num = 1;

RESPONSE
----------------------------------------
Response One

--
Cheers,
Chris

___________________________________

Chris Leonard, The Database Guy
http://www.databaseguy.com

Brainbench MVP for Oracle Admin
http://www.brainbench.com

MCSE, MCDBA, OCP, CIW
___________________________________

"R. Santiago" <su*****@rocketmail.com> wrote in message
news:f2**************************@posting.google.c om...
I have a table cc_rd_user_questions with the following columns:

NUM NUMBER (25) DEFAULT 1 NOT NULL,
COMPANY_ID NUMBER (15) NOT NULL,
PROJ_ID NUMBER (15) NOT NULL,
SEQ NUMBER (25) NOT NULL,
QUEST_ID NUMBER (15) NOT NULL,
RESPONSE VARCHAR2 (40),
CREATE_DATE DATE NOT NULL,
LAST_UPDATE DATE NOT NULL

This table contains the responses to questions found on a form. The
SEQ column is incremented each time a new row is inserted for a
specific company and specific project). If during an insert the new
RESPONSE column is null, what is the best method to populate the
RESPONSE column with the previous RESPONSE column value (the previous
row where COMPANY_ID, PROJ_ID and QUEST_ID are the same but SEQ = SEQ
- 1)? Should I use a trigger (I forsee a "mutating" table problem), a
temp table or SQL?

Thanks in advance!

- Rey

Jul 19 '05 #2
su*****@rocketmail.com (R. Santiago) wrote in message news:<f2**************************@posting.google. com>...
I have a table cc_rd_user_questions with the following columns:

NUM NUMBER (25) DEFAULT 1 NOT NULL,
COMPANY_ID NUMBER (15) NOT NULL,
PROJ_ID NUMBER (15) NOT NULL,
SEQ NUMBER (25) NOT NULL,
QUEST_ID NUMBER (15) NOT NULL,
RESPONSE VARCHAR2 (40),
CREATE_DATE DATE NOT NULL,
LAST_UPDATE DATE NOT NULL

This table contains the responses to questions found on a form. The
SEQ column is incremented each time a new row is inserted for a
specific company and specific project). If during an insert the new
RESPONSE column is null, what is the best method to populate the
RESPONSE column with the previous RESPONSE column value (the previous
row where COMPANY_ID, PROJ_ID and QUEST_ID are the same but SEQ = SEQ
- 1)? Should I use a trigger (I forsee a "mutating" table problem), a
temp table or SQL?

Thanks in advance!

- Rey


is there a reason why you don't want to just do something like

insert ... select ... decode(:new_response, null, (select response
from cc_rd_user_questions where <what you said>), :new_response) from
dual ?

however, what happens if that previous SEQ - 1 answer gets updated in
the future? do you need to look at the following answer and perhaps
change that as well? and could that cause a chain of new updates?
maybe it would be easier if a null RESPONSE was understood to mean
"see above" at query time, rather than filling it in at insert time.
Jul 19 '05 #3
Thanks for your suggestion Chris but this trigger needs to occur
during an insert not during an update.

I tried it on an insert and received the "mutating table" error.
"Chris Leonard" <s_***********@hotmail.com> wrote in message news:<JW****************@news.uswest.net>...
You can build your SQL so that it doesn't pass in the Response column if it
is null. I think this is the best solution. However, it is also possible
to use a trigger. It's a simple solution, and easy to implement, but it's
not as efficient or elegant as making sure your app just doesn't update the
Reponse column if it is null on the screen. Having said that, you don't
need to run into the mutating table problem, just use the :OLD and :NEW
aliases to set the value. For example:

SQL> create table cc_rd_user_questions (
2 num number(25) default 1 not null,
3 company_id number(15) not null,
4 seq number(25) not null,
5 quest_id number(15) not null,
6 response varchar2(40),
7 create_date date default sysdate not null,
8 last_update date default sysdate not null);

Table created.

SQL> list
1 create or replace trigger cc_rd_user_response_fixup_trig
2 before update
3 on cc_rd_user_questions
4 for each row
5 begin
6 if :new.response is null then
7 :new.response := :old.response;
8 end if;
9* end;
SQL> /

Trigger created.

SQL> insert into cc_rd_user_questions values (1, 2, 3, 4, 'Response One',
sysdate, sysdate);

1 row created.

SQL> select Response from cc_rd_user_questions where num = 1;

RESPONSE
----------------------------------------
Response One

SQL> update cc_rd_user_questions set Response=NULL where NUM=1;

1 row updated.

SQL> select Response from cc_rd_user_questions where num = 1;

RESPONSE
----------------------------------------
Response One

--
Cheers,
Chris

___________________________________

Chris Leonard, The Database Guy
http://www.databaseguy.com

Brainbench MVP for Oracle Admin
http://www.brainbench.com

MCSE, MCDBA, OCP, CIW
___________________________________

"R. Santiago" <su*****@rocketmail.com> wrote in message
news:f2**************************@posting.google.c om...
I have a table cc_rd_user_questions with the following columns:

NUM NUMBER (25) DEFAULT 1 NOT NULL,
COMPANY_ID NUMBER (15) NOT NULL,
PROJ_ID NUMBER (15) NOT NULL,
SEQ NUMBER (25) NOT NULL,
QUEST_ID NUMBER (15) NOT NULL,
RESPONSE VARCHAR2 (40),
CREATE_DATE DATE NOT NULL,
LAST_UPDATE DATE NOT NULL

This table contains the responses to questions found on a form. The
SEQ column is incremented each time a new row is inserted for a
specific company and specific project). If during an insert the new
RESPONSE column is null, what is the best method to populate the
RESPONSE column with the previous RESPONSE column value (the previous
row where COMPANY_ID, PROJ_ID and QUEST_ID are the same but SEQ = SEQ
- 1)? Should I use a trigger (I forsee a "mutating" table problem), a
temp table or SQL?

Thanks in advance!

- Rey

Jul 19 '05 #4
Hi.

I tried with this trigger and it works:

create or replace trigger T before insert on cc_rd_user_questions for each row
begin
if :new.response is null then
select response into :new.response from cc_rd_user_questions
where COMPANY_ID = :new.company_id
and PROJ_ID = :new.proj_id and QUEST_ID = :new.quest_id
and SEQ = :new.seq - 1;
end if;
exception
when no_data_found then null;
end;
/

Bye.

su*****@rocketmail.com (R. Santiago) wrote in message news:<f2**************************@posting.google. com>...
I have a table cc_rd_user_questions with the following columns:

NUM NUMBER (25) DEFAULT 1 NOT NULL,
COMPANY_ID NUMBER (15) NOT NULL,
PROJ_ID NUMBER (15) NOT NULL,
SEQ NUMBER (25) NOT NULL,
QUEST_ID NUMBER (15) NOT NULL,
RESPONSE VARCHAR2 (40),
CREATE_DATE DATE NOT NULL,
LAST_UPDATE DATE NOT NULL

This table contains the responses to questions found on a form. The
SEQ column is incremented each time a new row is inserted for a
specific company and specific project). If during an insert the new
RESPONSE column is null, what is the best method to populate the
RESPONSE column with the previous RESPONSE column value (the previous
row where COMPANY_ID, PROJ_ID and QUEST_ID are the same but SEQ = SEQ
- 1)? Should I use a trigger (I forsee a "mutating" table problem), a
temp table or SQL?

Thanks in advance!

- Rey

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: FizzBin | last post by:
We are writing a C application that is using ODBC to insert records into a database. We have a NOT NULL column that can legitimately have an empty value, i.e. we know the value and it is empty...
3
by: Tyler Hudson | last post by:
/*Code below raises following errors: Server: Msg 245, Level 16, State 1, Line 6 Syntax error converting the varchar value 'a' to a column of data type int. */ create table #x (i integer, c...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
6
by: fniles | last post by:
I am using VB.NET 2003 and SQL Server 2000. I have a table with a datetime column type. When inserting into the table for that column, I set it to Date.Now.ToString("T") , which is something like...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); testDataTable.Columns.Add("updatedDateTime",...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
4
priyan
by: priyan | last post by:
hai everyone, I am having a doubt in inserting data in time field. I am having a table in which in column in timestamp without time zone datatype. I want to insert a row into the table but...
0
by: miamikk | last post by:
I am XML newbie. I have question about inserting dynamic text in the header of HTML table. This is the site I have created (Only Report Type 1 is working)...
4
by: R. Santiago | last post by:
I have a table cc_rd_user_questions with the following columns: NUM NUMBER (25) DEFAULT 1 NOT NULL, COMPANY_ID NUMBER (15) NOT NULL, PROJ_ID NUMBER (15) NOT NULL, SEQ ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.