July 20, 2009 at 8:31 am
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
July 20, 2009 at 8:47 am
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.
July 20, 2009 at 9:04 am
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
July 20, 2009 at 9:17 am
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
July 20, 2009 at 9:26 am
Got it working thanks, it was the loginname column had some nulls, so I had to match on a different column. Dohh!
July 20, 2009 at 9:26 am
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
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply