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 |
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-07-11 : 13:21:21
|
I have an import and staging database where imports come from flat files and the imported data is appended to or merged with the stage data tables. I have determined we need created_on and modified_on columns in order to trace problems in the data. I know that you can have a default date to populate the tables initially. What I want is a trigger on each table that will not fire when the data is initially loaded, but only when and if the row is modified, particularly in the stage tables. The import tables are only a landing place for the flat file data. I am not sure when the triggers file even now on this simple script: USE SandboxGOCREATE TABLE [my_table] ( [id] [int] IDENTITY (1, 1) NOT NULL, [my_data] [varchar] (50) NULL, [created_on] [datetime] NOT NULL, [modified_on] [datetime] NOT NULL,) ON [PRIMARY]ALTER TABLE [my_table] WITH NOCHECK ADD CONSTRAINT [PK_my_table] PRIMARY KEY CLUSTERED ( [Id] ) ON [PRIMARY] GO ALTER TABLE [my_table] ADD CONSTRAINT [DF_my_table_created_on] DEFAULT (getdate()) FOR [created_on]GO ALTER TABLE [my_table] ADD CONSTRAINT [DF_my_table_modified_on] DEFAULT (getdate()) FOR [modified_on]GOCREATE TRIGGER trg_update_my_table on my_table FOR UPDATE AS BEGIN UPDATE my_table SET modified_on=getdate(), created_on=d.created_on FROM my_table INNER JOIN deleted d on my_table.id = d.idENDGOINSERT my_table (my_data) VALUES ('Nadine')INSERT my_table (my_data) VALUES ('Haddon')INSERT my_table (my_data) VALUES ('Alayna')INSERT my_table (my_data) VALUES ('Kaleigh')INSERT my_table (my_data) VALUES ('Evan')INSERT my_table (my_data) VALUES ('Payton')SELECT * FROM my_tableUPDATE my_table SET my_data = 'Nadine Cooper' WHERE my_data = 'Nadine'UPDATE my_table SET my_data = 'Haddon Smith' WHERE my_data = 'Haddon'UPDATE my_table SET my_data = 'Alayna Thomas' WHERE my_data = 'Alayna'UPDATE my_table SET my_data = 'Kaleigh Willliams' WHERE my_data = 'Kaleigh'UPDATE my_table SET my_data = 'Evan Roberts' WHERE my_data = 'Evan'UPDATE my_table SET my_data = 'Payton Jones' WHERE my_data = 'Payton'SELECT * FROM my_tableDROP my_table As an aside, my tables for the most part do not have primary keys. This was not my doing, but some ideas how to accomplish this without them would be helpful. In all this, I want to make sure i don't degrade performance as I have little experience with triggers. Thank you.Duane |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-07-11 : 14:08:01
|
This probably doesn't address your actual question, but why can't the import process handle updating these columns? Presumably, you (your company) is in control of this process and should be able to add that to the existing processing that is currently in place and avoid having to use triggers. |
 |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-07-11 : 14:14:47
|
quote: Originally posted by Lamprey This probably doesn't address your actual question, but why can't the import process handle updating these columns? Presumably, you (your company) is in control of this process and should be able to add that to the existing processing that is currently in place and avoid having to use triggers.
It can, but I am thinking about the stage tables - when I import everything from the import to the stage tables, I don't want the triggers on the stage tables to fire. I can either populate the stage tables with the dates from the import tables, or use getdate() as a default for them as well. I am thinking more of merged rows, or rows modified external to the import process that should fire the stage table triggers. I probably for sure don't need the triggers on the import tables at all, however.Duane |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-11 : 15:58:37
|
are you telling that your staging table data gets modified by external processes other than etl ones?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2012-07-11 : 16:25:43
|
quote: Originally posted by visakh16 are you telling that your staging table data gets modified by external processes other than etl ones?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Not necessarily, but I wouldn't be surprised if it happened at some point, even manual adjustments. In fact, just recently, I had corrupted data in one row (test database) and had to change it manually myself. I haven't used triggers before, but I think it could help in some of these issues to be able to nail down exactly when a row was modified. Even without this, it seems like it would be a good idea even from the etl standpoint. But I am open to suggestions.Duane |
 |
|
|
|
|
|
|