Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Instead Of Triggers on Views

$
0
0
Hi,This post is related to http://www.sqlservercentral.com/Forums/Topic1690200-3412-1.aspx.Here is some standalone, sample code:[code="sql"]-- Create Fact TableDROP TABLE [dbo].[fact_Money]GOCREATE TABLE [dbo].[fact_Money]( [MoneyID] [int] IDENTITY(1,1) NOT NULL, [DayID] [int] NOT NULL, [Money] [decimal](5,2) NOT NULL, CONSTRAINT [PK__fact_Money_MoneyID] PRIMARY KEY CLUSTERED ( [MoneyID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO-- Create Dimension TableDROP TABLE [dbo].[dim_Days]GOCREATE TABLE [dbo].[dim_Days]( [DayID] [int] IDENTITY(0,1) NOT NULL, [Day] [varchar](15) NOT NULL, CONSTRAINT [PK__dim_Days_DayID] PRIMARY KEY CLUSTERED ( [DayID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO-- Add Integrity ConstraintALTER TABLE [dbo].[fact_Money] WITH CHECK ADD CONSTRAINT [FK_fact_Money_dim_Days_DayID] FOREIGN KEY([DayID])REFERENCES [dbo].[dim_Days] ([DayID])GO-- Load TablesINSERT INTO [dbo].[dim_Days] (Day)VALUES('Sunday'),('Monday'),('Tuesday'),('Wednesday'),('Thursday'),('Friday'),('Saturday')GOINSERT INTO [dbo].[fact_Money] (DayID, Money)VALUES(0,0),(0,$100),(0,$200),(1,$300),(1,$400),(2,$500),(2,$600),(2,$700),(0,$800),(1,$900)GO-- Create viewDROP VIEW [dbo].[vfact_Money]GOCREATE VIEW [dbo].[vfact_Money] ASSELECT m.MoneyID, m.DayID, d.Day, m.MoneyFROM [dbo].[fact_Money] mINNER JOIN [dbo].[dim_Days] dON m.DayID=d.DayIDGO[/code]Now, open the view in SSMS, edit top 200 rows. Edit as follows:1) Edit row 1, MoneyID=1, change DayID from 0 to 1.2) Refresh the view - day has changed from Sunday to Monday3) View the data in fact_Money. So far, so good.4) Edit row 8, MoneyID=8, change Day from Tuesday to Thursday5) Refresh the view - ALL "Tuesdays" are not "Thursdays"6) View the data in dim_Days. The dimension table has changed. In some scenarios this would be desired - in this scenario it's definitely NOT what I want.Is there a way that I can edit the Day column in the view, but have that action cause the DayID column to be written to the fact table instead?I'm wondering if this approach can be used: http://michaeljswart.com/2012/10/triggers-on-views-what-for/. If so, and you can provide a simple example, that would be great. Would I need a separate trigger for each column, or a single "instead of update" trigger for all the other dimension table columns?If this approach does not work, or is not best practice, can you suggest a better approach? Perhaps my Excel FE would update a staging/temp/utility table under the covers, then calls a SP (or perhaps that staging table has a trigger?) to update the fact table? Then I refresh the view in Excel?Thanks for any help you can provide.Regards,Scott

Viewing all articles
Browse latest Browse all 3145

Trending Articles