My table structure is below:
CREATE TABLE [ACC].[Document](
[DocumentID] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NOT NULL,
[SalesCompanyFinancialPeriodID] [int] NOT NULL,
[DocumentTypeID] [int] NULL,
[Number] [int] NULL,
[Title] [nvarchar](200) NULL,
[ConfirmStatusEnumID] [int] NULL,
[SignedPrice] [money] NOT NULL,
CONSTRAINT [PK_Document] PRIMARY KEY CLUSTERED
(
[DocumentID] ASC
)
I want to set Number column value automaticaly increment number started from 1 for each SalesCompanyFinancialPeriodID column. In other word if new record inserted in this table, value of Number column calculated in instead of trigger, such as below:
ISNULL( (Select Max(A.Number)
From Acc.Document A
Where A.SalesCompanyFinancialPeriodID = Inserted.SalesCompanyFinancialPeriodID),1)
What is the best practices for this problem.
instead oftrigger? – dezso Jun 7 '12 at 6:22instead oftriggers are evil? Are you suggesting an after trigger is better? Do you think it's better to insert a row and then update the value, rather than just insert the intact row once? Do you think it's better to perform business logic and validation after you've inserted the row, so if validation fails you have to do the work twice (insert and then rollback) instead of zero times? – Aaron Bertrand Jun 7 '12 at 6:24AFTERtriggers only for modifyling other tables based on the row in question. The problem is apparently in my box so to say -INSTEAD OFtriggers can be defined only on views in Oracle an PostgreSQL, and there areBEFOREtriggers for doing things like the OP wants to accomplish. So +1 for your comment, because I was unaware of this difference between RDBMSes. – dezso Jun 7 '12 at 7:31