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 |
|
label
Posting Yak Master
197 Posts |
Posted - 2003-04-07 : 10:12:01
|
I'd like to write a trigger that does the following, unfortuantely, I'm not particularly versed in using Triggers. 1. When a new record is added to the table("Download_Sections"), I want a trigger to figure out what the largest "Section_Order" is from all of the other records in that table with the same "Page_ID" as the record I just added. 2. I then want to take that number, add "1" to it and then update the record I just inserted with the new number. The table I want to do this to is this: quote: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Download_Links_Download_Sections]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[Download_Links] DROP CONSTRAINT FK_Download_Links_Download_SectionsGOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Download_Sections]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Download_Sections]GOCREATE TABLE [dbo].[Download_Sections] ( [Section_ID] [int] IDENTITY (1, 1) NOT NULL , [Page_ID] [int] NOT NULL , [Section_Title] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Section_Description] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Section_Order] [int] NULL , [Created_By] [int] NULL , [Created_On] [datetime] NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[Download_Sections] WITH NOCHECK ADD CONSTRAINT [PK_Download_Sections] PRIMARY KEY CLUSTERED ( [Section_ID] ) ON [PRIMARY] GOALTER TABLE [dbo].[Download_Sections] WITH NOCHECK ADD CONSTRAINT [DF_Download_Sections_Created_On] DEFAULT (getdate()) FOR [Created_On]GO CREATE INDEX [IX_Download_Sections] ON [dbo].[Download_Sections]([Page_ID]) ON [PRIMARY]GOALTER TABLE [dbo].[Download_Sections] ADD CONSTRAINT [FK_Download_Sections_Download_Pages] FOREIGN KEY ( [Page_ID] ) REFERENCES [dbo].[Download_Pages] ( [Page_ID] ) ON DELETE CASCADE NOT FOR REPLICATION GO
Thanks in advance. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-07 : 10:54:04
|
| create trigger tr_Download_Sections_ins on Download_Sections for insertasupdate Download_Sectionsset Section_Order = (select coalesce(max(Section_Order),0) + 1 from Download_Sections ds where i.Page_ID = ds.Page_ID)from Download_Sections ds, inserted iwhere i.Section_ID = ds.Section_IDgoif you want to cope with multiple entries in one statementupdate Download_Sectionsset Section_Order = (select coalesce(max(Section_Order),0) + 1 from Download_Sections ds where i.Page_ID = ds.Page_ID)+ (select count(*) from inserted i2 where i2.Page_ID = i.Page_ID and i2.Section_ID < i.Section_ID)from Download_Sections ds, inserted iwhere i.Section_ID = ds.Section_ID==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.Edited by - nr on 04/07/2003 10:57:31Edited by - nr on 04/07/2003 11:09:56 |
 |
|
|
label
Posting Yak Master
197 Posts |
Posted - 2003-04-07 : 11:13:01
|
quote: create trigger tr_Download_Sections_ins on Download_Sections for insertasupdate Download_Sectionsset Section_Order = (select coalesce(max(Section_Order),0) + 1 from Download_Sections ds where i.Page_ID = ds.Page_ID)from Download_Sections ds, inserted iwhere i.Section_ID = ds.Section_IDgo
Thanks, this worked perfectly, though I have a question if you don't mind. Is "coalesce" in there because your excluding all Null values from this statement, is that correct? Thanks again. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-07 : 11:30:29
|
| Yep. If this is the first entry for that Page_id then the max will be null (it all assumes you add the row with null) so it will give 1 for the first.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|