473,770 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting default value by "trigger"

I have a case where I am collecting a "Start Date" and an "End Date".
I would like to default the "End Date" to the "Start Date" value if only
the "Start Date" is entered.
I tried setting this as default on the table, but it was not permitted.
So, now I am trying to figure out how to do it with a trigger or
trigger/function combination.

I tried doing it with a rule using "INSTEAD" on INSERT, but that gave an
"endless recursion" error, which did make ssense after I looked at it
more closely.

I also tried making a trigger, which called a function that changed the
value of the NEW.enddate to the NEW.startdate value if NEW.enddate is
NULL, but I got an error saying that the NEW. values were not available
yet (doing trigger BEFORE INSERT).

Has anyone else done something like this, and if so, what approach
worked for you?
Thanks.
Barb

--
Barbara E. Lindsey,
COG RDC
Phone: (352) 392-5198 ext. 314 Fax: (352) 392-8162

----
CONFIDENTIALITY NOTICE: The information contained in this electronic
message is legally privileged and confidential and intended only for the
use of the individual(s) or entity(ies) named above. If the reader of
this message is not the intended recipient, you are hereby notified that
any dissemination, distribution, or copying of this email or any of it's
components is strictly prohibited. If you have received this email in
error, please contact the sender.
----
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 22 '05 #1
3 3363
This sure looks a lot like what I already tried, but I will try it again...

d L. wrote:
This works on 7.3.4:

CREATE TABLE foo (id SERIAL, starttime TIMESTAMP, endtime TIMESTAMP);

CREATE FUNCTION adjust_end_time () RETURNS "trigger" AS '
BEGIN
IF NEW.endtime ISNULL THEN
NEW.endtime := NEW.starttime;
END IF;
RETURN NEW;
END;'
LANGUAGE plpgsql;

CREATE TRIGGER foo_trigger
BEFORE INSERT ON foo
FOR EACH ROW
EXECUTE PROCEDURE adjust_end_time ();

INSERT INTO foo(starttime, endtime) VALUES (now(), now());
INSERT INTO foo(starttime) VALUES (now());

SELECT * FROM foo;

On Monday February 9 2004 9:24, Barbara Lindsey wrote:
I have a case where I am collecting a "Start Date" and an "End Date".
I would like to default the "End Date" to the "Start Date" value if only
the "Start Date" is entered.
I tried setting this as default on the table, but it was not permitted.
So, now I am trying to figure out how to do it with a trigger or
trigger/function combination.

I tried doing it with a rule using "INSTEAD" on INSERT, but that gave an
"endless recursion" error, which did make ssense after I looked at it
more closely.

I also tried making a trigger, which called a function that changed the
value of the NEW.enddate to the NEW.startdate value if NEW.enddate is
NULL, but I got an error saying that the NEW. values were not available
yet (doing trigger BEFORE INSERT).

Has anyone else done something like this, and if so, what approach
worked for you?
Thanks.
Barb


--
Barbara E. Lindsey,
COG RDC
Phone: (352) 392-5198 ext. 314 Fax: (352) 392-8162

----
CONFIDENTIALITY NOTICE: The information contained in this electronic
message is legally privileged and confidential and intended only for the
use of the individual(s) or entity(ies) named above. If the reader of
this message is not the intended recipient, you are hereby notified that
any dissemination, distribution, or copying of this email or any of it's
components is strictly prohibited. If you have received this email in
error, please contact the sender.
----
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #2
This works on 7.3.4:

CREATE TABLE foo (id SERIAL, starttime TIMESTAMP, endtime TIMESTAMP);

CREATE FUNCTION adjust_end_time () RETURNS "trigger" AS '
BEGIN
IF NEW.endtime ISNULL THEN
NEW.endtime := NEW.starttime;
END IF;
RETURN NEW;
END;'
LANGUAGE plpgsql;

CREATE TRIGGER foo_trigger
BEFORE INSERT ON foo
FOR EACH ROW
EXECUTE PROCEDURE adjust_end_time ();

INSERT INTO foo(starttime, endtime) VALUES (now(), now());
INSERT INTO foo(starttime) VALUES (now());

SELECT * FROM foo;

On Monday February 9 2004 9:24, Barbara Lindsey wrote:
I have a case where I am collecting a "Start Date" and an "End Date".
I would like to default the "End Date" to the "Start Date" value if only
the "Start Date" is entered.
I tried setting this as default on the table, but it was not permitted.
So, now I am trying to figure out how to do it with a trigger or
trigger/function combination.

I tried doing it with a rule using "INSTEAD" on INSERT, but that gave an
"endless recursion" error, which did make ssense after I looked at it
more closely.

I also tried making a trigger, which called a function that changed the
value of the NEW.enddate to the NEW.startdate value if NEW.enddate is
NULL, but I got an error saying that the NEW. values were not available
yet (doing trigger BEFORE INSERT).

Has anyone else done something like this, and if so, what approach
worked for you?
Thanks.
Barb

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #3
Ahh! I didn't have the RETURN NEW; line in the function.
Thanks!
--------------------------------------------------------------------------------

Ed L. wrote:
This works on 7.3.4:

CREATE TABLE foo (id SERIAL, starttime TIMESTAMP, endtime TIMESTAMP);

CREATE FUNCTION adjust_end_time () RETURNS "trigger" AS '
BEGIN
IF NEW.endtime ISNULL THEN
NEW.endtime := NEW.starttime;
END IF;
RETURN NEW;
END;'
LANGUAGE plpgsql;

CREATE TRIGGER foo_trigger
BEFORE INSERT ON foo
FOR EACH ROW
EXECUTE PROCEDURE adjust_end_time ();

INSERT INTO foo(starttime, endtime) VALUES (now(), now());
INSERT INTO foo(starttime) VALUES (now());

SELECT * FROM foo;

On Monday February 9 2004 9:24, Barbara Lindsey wrote:
I have a case where I am collecting a "Start Date" and an "End Date".
I would like to default the "End Date" to the "Start Date" value if only
the "Start Date" is entered.
I tried setting this as default on the table, but it was not permitted.
So, now I am trying to figure out how to do it with a trigger or
trigger/function combination.

I tried doing it with a rule using "INSTEAD" on INSERT, but that gave an
"endless recursion" error, which did make ssense after I looked at it
more closely.

I also tried making a trigger, which called a function that changed the
value of the NEW.enddate to the NEW.startdate value if NEW.enddate is
NULL, but I got an error saying that the NEW. values were not available
yet (doing trigger BEFORE INSERT).

Has anyone else done something like this, and if so, what approach
worked for you?
Thanks.
Barb


--
Barbara E. Lindsey,
COG RDC
Phone: (352) 392-5198 ext. 314 Fax: (352) 392-8162

----
CONFIDENTIALITY NOTICE: The information contained in this electronic
message is legally privileged and confidential and intended only for the
use of the individual(s) or entity(ies) named above. If the reader of
this message is not the intended recipient, you are hereby notified that
any dissemination, distribution, or copying of this email or any of it's
components is strictly prohibited. If you have received this email in
error, please contact the sender.
----
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #4

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

Similar topics

4
2504
by: Tony | last post by:
Is there any known SQL Server bug whereby a record can be successfully inserted and committed, but then later be found not to be in the database? For example, if there was a server crash just after the commit, could committed data be lost? I'm sure the answer must be "no", but a client is telling me this is happening, and I said I'd enquire.
2
5259
by: Jeff Magouirk | last post by:
Dear All, I have written an update trigger that should write a message to an audit table When I try to update any field in the table I recieve the following error message - Stirng or Binary data would be trunicated The statement has been termined.
8
3467
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net? Thanks! Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
4
7465
by: Tero Partanen | last post by:
Hello! I'm writing about a rather peculiar problem I'm having with Access2000. I have a table in which I have created one hyperlink-type field. I have given the field a default value which is an email-address. The default value is (for example): "mailto:mymail@here.com". Access2000 correctly inserts that information into the field everytime
13
2394
by: Morgan Bachu | last post by:
Hello everybody, I have an intranet application which is mostly a bunch of data editing forms. The database has about 20 tables all related together. Recently somebody deleted a "the wrong record" and the cascade delete in sql server did its work nicely :(
15
8592
by: cj | last post by:
How can I get a button in VB to send the contents of a text box via email in a manner similar to the "Send To\Mail Recipient" functionality that you can select via right clicking a file in Windows Explorer? I want the user to click a button and it lunch the users default email client and put the contents of a multi line text box in the body of the message and the contents of another text box in the title box and be sitting there read for...
4
6846
by: Lucky | last post by:
hi guys! Currently i'm facing a bit odd situation. i cant modify my code so i have to make changes in SQL Server. the problem is, i've modified one table by adding new column and now i want only those row which has not null value in that column whenever the select statement gets fire. i mean in every select statement only those rows should be returned which has NOT NULL value in the newly added column.
2
14255
by: PW | last post by:
Hi, What the heck is that supposed to mean? I am getting this error on a "Me.Requery" line in a subroutine on a form, but only when I select something from a combo/dropdown box. The *exact* same code is run when I do other things (like press the Save button or tab through that control). I have searched every control, involved queries and tables and I can't find anything that looks fishy.
5
3584
by: Ion | last post by:
Hi, I get SQL0746N when trying to call stored procedure. In my particular case I the message complains about operation "READ", but I'm posting a simplified version that results in SQL0746N with "MODIFY". I assume there is a similar reason, but I'm not quite sure what I'm doing wrong here. Any idea? I'm running 9.5 SP 0 on windows. Thank you! create table t(Id int not null primary key generated by default as
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.