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 |
|
nbourre
Starting Member
14 Posts |
Posted - 2009-01-04 : 00:29:08
|
Hi,This is my first post and I need some little help here. I'm a newbie in SQL triggers.In this problem I have 2 tables one named sectors and the second named sites.Each site belongs to a sector. The relationship is done with the sites.pkSectorID and the sectors.fkSectorID.Each sector has a unique code.Each site has an ID (siteID) which is entered by the user. If the user doesn't provide a siteID a default value is created by concatenating the sector unique code with the sites.pkSiteID.Each site ID must be unique.I created the following trigger but all I get is null values. What am I doing wrong?ALTER TRIGGER tg_new_siteIDON dbo.sitesAFTER INSERTAS if (select count(*) from inserted where siteID = '') = 1 begin UPDATE sites SET sites.siteID = (SELECT sectors.sectorCode FROM sectors WHERE sectors.pkSectorID = (SELECT fkSectorID FROM inserted)) + (SELECT fkSectorID FROM inserted) WHERE sites.pkSiteID = (SELECT fkSectorID FROM inserted) end Thanx in advanceNickBeginning with something is a good start! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-04 : 03:44:36
|
| [code]ALTER TRIGGER tg_new_siteIDON dbo.sitesAFTER INSERTASBEGIN UPDATE sSET s.siteID = sr.sectorCode +i.fkSectorIDFROM sites sJOIN inserted iON i.fkSectorID=s.pkSiteIDJOIN sectors srON sr.pkSectorID=i.fkSectorIDWHERE s.siteID = ''end[/code] |
 |
|
|
|
|
|