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 |
|
romanseaa
Starting Member
3 Posts |
Posted - 2010-10-05 : 04:07:49
|
| Hi,I am new to SQL 2008.For the past 3 days, I have been battling with TRIGGERS.In my database, I have 2 tables : one is called "Details" (which includes : detailsid,personid,surname, firstname).The second table is "Mainaddress" (addressid, personid, street, city).These 2 tables share only one common attribute : personidNext, I created a VIEW for these 2 tables, as follows :SELECT dbo.mainaddress.street, dbo.mainaddress.cityFROM dbo.mainaddress INNER JOIN dbo.Details ON dbo.mainaddress.personid = dbo.Details.personidBut, as we all know, it is not possible to manipulate more than one base table when using VIEWS.To solve this problem, I decided to create TRIGGERS. I created 3 triggers : one each for INSERT, UPDATE and DELETE.But, for some reason, I keep getting the error :INVALID COLUMN NAME ("column name")I have searched far and wide for a solution : forums, Google, etc, but without success.For example : here is my INSERT trigger.SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOcreate trigger IdM_tunnukset_insert on IdM_tunnukset instead of insert AS BEGIN SET NOCOUNT ON;insert into mainaddressselect addressid,personid,street,city from insertedinsert into detailsselect details_id,personid,surname,firstname from insertedENDGOBut, I kept getting the error :INVALID COLUMN NAME addressid;INVALID COLUMN NAME personid;etc, etc, etc. For EVERY of my columns.So, I decided to DECLARE the columns as follows :SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOcreate trigger dbo.IdM_tunnukset_insert on dbo.IdM_tunnukset instead of insert AS BEGIN SET NOCOUNT ON;declare @new_addressid nvarchar(15)declare @new_personid nvarchar(10)declare @new_street nvarchar(50)declare @new_city nvarchar(20)declare @new_surname nvarchar(50)declare @new_firstname nvarchar(50)declare @new_details_id nchar(12)select @new_addressid=addressid from insertedselect @new_personid=personid from inserted)select @new_street=street from inserted select @new_city=city from insertedselect @new_surname=surname from inserted select @new_firstname=firstname from inserted select @new_details_id=details_id from inserted insert into mainaddressselect addressid,personid,street,city from insertedinsert into detailsselect details_id,personid,surname,firstname from insertedENDGOBut, still the error persists!What am I doing wrong??Any help would be greatly appreciated.Thanks |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-05 : 04:45:11
|
quote: SELECT dbo.mainaddress.street, dbo.mainaddress.cityFROM dbo.mainaddress INNER JOIN dbo.Details ON dbo.mainaddress.personid = dbo.Details.personid
I understand that you are inserting data through view but in view defination you have not included the columns which you are using inside the trigger Ex: details_id,personid,surname,firstname You need to have these columns and other columns used in trigger too. |
 |
|
|
romanseaa
Starting Member
3 Posts |
Posted - 2010-10-05 : 06:39:57
|
| Thank you very much, pk_bohrathat was a rather silly mistake on my part )) |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-05 : 06:48:33
|
You are welcome |
 |
|
|
|
|
|
|
|