|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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> |
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
||||
|
||||
|
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))
|
|
#5
|
|||
|
|||
|
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? |
|
#6
|
|||
|
|||
|
... 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:
You can read more at: http://publib.boulder.ibm.com/infocenter/db2v7luw/index.jsp?topic=/com.ibm.db2v7.doc/db2s0/sqls0623.htm |
|
#7
|
||||
|
||||
|
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. |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Triggers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|