|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146,
Visits: 613
|
|
I've written the following update trigger on my local machine, and i've tested it and works fine, but when I try to do in production, it doesn't work. I'm not sure what else to look at. I'm just updating a couple of phone extension columns on a different database. Any ideas.
ALTER TRIGGER [trgUpdateExt] ON [dbo].[tblUser] FOR UPDATE AS DECLARE @ext varchar(5) DECLARE @user varchar(50)
SELECT @ext = ext, @user = loginname FROM dbo.tblUser
BEGIN UPDATE test1.dbo.tblUser Set ext = @ext, ext2 = @ext where loginname = @user
UPDATE test1.dbo.tblUser2 Set ext = @ext, ext2 = @ext where loginname = @user END
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
|
|
What doesn't work? By that I mean what behavior is occurring. The trigger looks fine syntactically, but it won't work if multiple rows are updated.
Instead what you'd do is join the tblUser to the inserted table and set the values.
The inserted table will have the same structure as tblUser and contain only the new values of the updated rows. So you can join on the PK (or the user) and update the extension.
Follow me on Twitter: @way0utwest
 Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146,
Visits: 613
|
|
Its not updating multiple rows. Its updating multiple columns on a row. Don't know if that makes a difference. I tried the following, but the update across the db is still not occurring. Again, it works fine in my test environment, just not production.
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go
ALTER TRIGGER [trgUpdateExt] ON [dbo].[tblUser] FOR UPDATE AS DECLARE @ext varchar(5) DECLARE @user varchar(50)
SELECT @ext = ext, @user = loginname FROM inserted i inner join dbo.tblUser tu on tu.loginname = i.loginame
print @ext print @user
BEGIN UPDATE test1.dbo.tblUser Set ext = @ext, ext2 = @ext where loginname = @user
UPDATE test1.dbo.tblUser2 Set ext = @ext, ext2 = @ext where loginname = @user END
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 4:59 PM
Points: 11,792,
Visits: 28,078
|
|
could it be security? does the uer who is inserting/updating into the table also have insert rights on the table in the other database? maybe these specific statements need EXECUTE AS to make them occur correcty:
UPDATE test1.dbo.tblUser Set ext = @ext, ext2 = @ext where loginname = @user
UPDATE test1.dbo.tblUser2 Set ext = @ext, ext2 = @ext where loginname = @user
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146,
Visits: 613
|
|
| Got it working thanks, it was the loginname column had some nulls, so I had to match on a different column. Dohh!
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
|
|
If it was security, I'd expect that it wouldn't update the original table and a rollback would occur. Is that what is happening?
Also, I'd write it in case multiple rows are updated. If it never happens, you haven't lost anything. If it does, then you've written bad code, and it doesn't take any longer to write:
UPDATE test1.dbo.tblUser Set ext = @ext, ext2 = @ext where loginname = @user than it does to write:
UPDATE a Set ext = t1.ext, ext2 = t1.ext from inserted i inner join test1.dbo.tblUser a on a.loginname = i.loginname
Follow me on Twitter: @way0utwest
 Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|