Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 16th, 2004, 09:14 AM
mats mats is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: sweden
Posts: 10 mats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Triggers

Hello!

Is it possible to send input parrameters to a trigger.

I whant to get the ID from the row previousy updated in a table in my datbase,
Then use that ID when executing a trigger on the table.

Thank you in advance.

Best regards

/mats

Reply With Quote
  #2  
Old March 16th, 2004, 09:32 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
How are you determining the previously updated rows ID?
You could create it something like this.
Code:
CREATE TRIGGER <TriggerName> ON <TableName OR ViewName>
FOR INSERT, UPDATE, DELETE
AS
SELECT [ID]
FROM <TableName>
WHERE <You determine the criteria for getting the ID>
<Then what ever you want to do with the ID>

Reply With Quote
  #3  
Old March 16th, 2004, 09:47 AM
mats mats is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: sweden
Posts: 10 mats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Memnoch
How are you determining the previously updated rows ID?
You could create it something like this.
Code:
CREATE TRIGGER <TriggerName> ON <TableName OR ViewName>
FOR INSERT, UPDATE, DELETE
AS
SELECT [ID]
FROM <TableName>
WHERE <You determine the criteria for getting the ID>
<Then what ever you want to do with the ID>

Thanks!

I´m sorry what a was really suposed to ask was this:
(the same thing you asked me)

* How do I determine the newly updated row?
* can I use that row´s ID (or any value in the row) within the trigger?

Sorry about the first unspecified question, partly due to bad english.

/mats

Reply With Quote
  #4  
Old March 16th, 2004, 10:05 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
1) well, The easiest way is to have an additional database field called LastEditDate of SmallDateTime data type. When a record is edited, this LastEditDate is then set to the date the edit happened. This way all you have to do is a select statement to find the record with the most current (MAX)LastEditDate, which will be the most recently updated record.
Code:
SELECT [ID]
FROM <TableName>
WHERE LastEditDate =
(SELECT MAX(LastEditDate)
FROM <tableName>)

So your Trigger could look like this
Code:
CREATE TRIGGER <TriggerName> ON <TableName OR ViewName>
FOR INSERT, UPDATE, DELETE
AS
INSERT INTO <AnotherTable>([ID]) VALUES(
   SELECT A.[ID]
      FROM <TableName> As A
      WHERE A.LastEditDate =
         (SELECT MAX(B.LastEditDate)
         FROM <tableName> As B))

Reply With Quote
  #5  
Old March 9th, 2005, 11:21 AM
swingheim swingheim is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 swingheim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m
Reputation Power: 0
Exclamation That won't work ...

In an environment where you have thousands of records being updated constantly, that approach will NOT work ...

Instead, there should be a way to strictly reference the record that caused the trigger to fire.

For example, when I UPDATE a record with an ID = 56, I should be able to reference a variable in the trigger that dynamically points to the record id of the record that fired the trigger in the first place:

UPDATE table SET column = value WHERE id = @id

WHERE the @id variable would reference the id of the record that fired the trigger.

The question that Mats was asking from the get-go, was "What is the name of that variable?"

(And this happens to be what I am looking for as well)

I was looking around the @@ variables, to see if I could find one ... I know that @@identity will return the ID of the last record INSERTED into the table ... I wish there was one that worked inside a trigger which returned the ID of the record that was UPDATED. Anyone?

Reply With Quote
  #6  
Old March 9th, 2005, 11:27 AM
swingheim swingheim is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 swingheim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m
Reputation Power: 0
... I think I found the solution. You can reference all the records that were affected by the UPDATE/INSERT/DELETE command inside the trigger by using referencing:

Code:
  REFERENCING Specifies the correlation names for the transition variables and the table names for the transition tables. Correlation names identify a specific row in the set of rows affected by the triggering SQL operation. Table names identify the complete set of affected rows. Each row affected by the triggering SQL operation is available to the triggered action by qualifying columns with correlation-names specified as follows. OLD AS correlation-name Specifies a correlation name which identifies the row state prior to the triggering SQL operation. NEW AS correlation-name Specifies a correlation name which identifies the row state as modified by the triggering SQL operation and by any SET statement in a BEFORE trigger that has already executed. The complete set of rows affected by the triggering SQL operation is available to the triggered action by using a temporary table name specified as follows. 
 
 OLD_TABLE AS identifier Specifies a temporary table name which identifies the set of affected rows prior to the triggering SQL operation. NEW_TABLE AS identifier Specifies a temporary table name which identifies the affected rows as modified by the triggering SQL operation and by any SET statement in a BEFORE trigger that has already executed. The following rules apply to the REFERENCING clause:
  • None of the OLD and NEW correlation names and the OLD_TABLE and NEW_TABLE names can be identical (SQLSTATE 42712).
  • Only one OLD and one NEW correlation-name may be specified for a trigger (SQLSTATE 42613).
  • Only one OLD_TABLE and one NEW_TABLE identifier may be specified for a trigger (SQLSTATE 42613).
  • The OLD correlation-name and the OLD_TABLE identifier can only be used if the trigger event is either a DELETE operation or an UPDATE operation (SQLSTATE 42898). If the operation is a DELETE operation, OLD correlation-name captures the value of the deleted row. If it is an UPDATE operation, it captures the value of the row before the UPDATE operation. The same applies to the OLD_TABLE identifier and the set of affected rows.
  • The NEW correlation-name and the NEW_TABLE identifier can only be used if the trigger event is either an INSERT operation or an UPDATE operation (SQLSTATE 42898). In both operations, the value of NEW captures the new state of the row as provided by the original operation and as modified by any BEFORE trigger that has executed to this point. The same applies to the NEW_TABLE identifier and the set of affected rows.
  • OLD_TABLE and NEW_TABLE identifiers cannot be defined for a BEFORE trigger (SQLSTATE 42898).
  • OLD and NEW correlation-names cannot be defined for a FOR EACH STATEMENT trigger (SQLSTATE 42899).
  • Transition tables cannot be modified (SQLSTATE 42807).
  • The total of the references to the transition table columns and transition variables in the triggered-action cannot exceed the limit for the number of columns in a table or the sum of their lengths cannot exceed the maximum length of a row in a table (SQLSTATE 54040).
  • The scope of each correlation-name and each identifier is the entire trigger definition.
FOR EACH ROW Specifies that the triggered action is to be applied once for each row of the subject table that is affected by the triggering SQL operation. FOR EACH STATEMENT Specifies that the triggered action is to be applied only once for the whole statement. This type of trigger granularity cannot be specified for a BEFORE trigger (SQLSTATE 42613). If specified, an UPDATE or DELETE trigger is activated even when no rows are affected by the triggering UPDATE or DELETE statement.


You can read more at:

http://publib.boulder.ibm.com/infocenter/db2v7luw/index.jsp?topic=/com.ibm.db2v7.doc/db2s0/sqls0623.htm

Reply With Quote
  #7  
Old March 9th, 2005, 11:41 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You realize this thread is a year old right?
I'm sure he's found his solution by now.

In the future, please don't resurrect dead threads.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Triggers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT