Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2005-07-27 : 19:15:21
|
| CREATE TRIGGER trgInsertProjectON ProjectFOR INSERTASSET NOCOUNT ON DECLARE @ProjectID intDECLARE @SeasonID intDECLARE @ProductCategoryID intDECLARE @CompanyID intDECLARE @ShipDate datetimeDECLARE my_cursor CURSOR STATIC FORSELECT ShipDate, ISNULL(CompanyID, -1) AS CompanyID, ISNULL(SeasonID, -1) AS SeasonID, ISNULL(ProductCategoryID, -1) AS ProductCategoryID, ProjectIDFROM Project WHERE DateCreated>=DATEADD(s,-5,GetDate())OPEN my_cursorFETCH NEXT FROM my_cursor INTO @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectIDWHILE @@FETCH_STATUS = 0BEGIN EXEC pCreateProjectEvents @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectID FETCH NEXT FROM my_cursor INTO @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectIDENDCLOSE my_cursorDEALLOCATE my_cursorGO |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2005-07-27 : 19:16:49
|
Forgot the question above...How can I ensure that this trigger fires for ONLY the ProjectIDs that were in the transaction? I mean, what if two user insert at 5 seconds apart. According to my code, there is a chance that they may run into one another, and both set of IDs will get called from within BOTH transactions!! Is there any way to appropriately lock the table, so that it is only released AFTER the stored proc runs AND the trigger is called for the appropriate ProjectIDs? Thanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-27 : 19:18:17
|
| You need to use the inserted table instead of Project. Check SQL Server Books Online CREATE TRIGGER for information on inserted and deleted tables. These tables exists while in the trigger.Tara |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2005-07-27 : 19:21:26
|
quote: Originally posted by tduggan You need to use the inserted table instead of Project. Check SQL Server Books Online CREATE TRIGGER for information on inserted and deleted tables. These tables exists while in the trigger.Tara
But I think I need to lock BOTH tables...right? The data I am selecting FROM is the Project table, the data I am inserting TO is the ProjectEvent table. So what if two users insert records within seconds of each other? How do I lock the records so what I don't run into problems? Or am I way off base here. Thanks TaraHC |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-27 : 19:24:20
|
| No, you don't lock Project. The inserted table contains the rows that are being inserted. You do not need to go to the Project table to find out what was just inserted. That's what the inserted table is for!Tara |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2005-07-27 : 20:53:08
|
quote: Originally posted by tduggan No, you don't lock Project. The inserted table contains the rows that are being inserted. You do not need to go to the Project table to find out what was just inserted. That's what the inserted table is for!Tara
deleted and inserted are logical (conceptual) tables. They are structurally similar to the table on which the trigger is defined, that is, the table on which the user action is attempted, and hold the old values or new values of the rows that may be changed by the user action. So, change my Trigger toCREATE TRIGGER trgInsertProject ON Project FOR INSERT AS SET NOCOUNT ON DECLARE @ProjectID int DECLARE @SeasonID int DECLARE @ProductCategoryID int DECLARE @CompanyID int DECLARE @ShipDate datetime DECLARE my_cursor CURSOR STATIC FOR SELECT ShipDate, ISNULL(CompanyID, -1) AS CompanyID, ISNULL(SeasonID, -1) AS SeasonID, ISNULL(ProductCategoryID, -1) AS ProductCategoryID, ProjectID FROM insertedOPEN my_cursor FETCH NEXT FROM my_cursor INTO @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectID WHILE @@FETCH_STATUS = 0 BEGIN EXEC pCreateProjectEvents @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectID FETCH NEXT FROM my_cursor INTO @ShipDate, @CompanyID, @SeasonID, @ProductCategoryID, @ProjectID END CLOSE my_cursor DEALLOCATE my_cursor That seems too easy...but works like a charm! Thanks Tara! |
 |
|
|
|
|
|
|
|