Re: Trigger for update

From: David Gugick (davidg-nospam_at_imceda.com)
Date: 11/28/04


Date: Sun, 28 Nov 2004 00:23:09 -0500

maryam rezvani wrote:
> Hi
>
> I've following structure in Sql server 2000,
>
> in my db called db1 I have a table called EMP with 3 columns
> (EMPID,EMPNAME,EMPDESC)
> which 2 logins has access to modify this DB and specially EMP, the
> user with login1 called(L1)
> can update only the EMPNAME and the user with login2 called(L2)can
> update only EMPDESC,
> and I wrote a trigger for updating on EMP to update a 2th table in
> DB2 to copy the new changes
> for EMPNAME in destination,(NOTE: only L1 has access to DB2 )
>
> DB1 : EMP (EMPID,EMPNAME,EMPDESC) ->L1,L2
> DB2: EMP (EMPID,EMPNAME) ->L1
> the trigger works successfully when I make new changes in empname
> through the L1,
> but when I want to update the value of empdesc through L2,an error
> will be appearred that
> you have no permission to DB2,and it's true,
> Now,how can I solve my problem without permitting L2 to have an
> access to DB2?
> in another word,how can I check in my trigger whether EMPDESC has
> updated take no action,
> and if EMPNAME has updated do the text of trigger,
>
> Any help would be greatly thankful.

Since you are dealing with a cross database query, you can check the
user name using the suser_sname() function in the trigger and only
perform the insert if it's the correct user.

You can use the IS_MEMBER ( { 'group' | 'role' } ) function to see if
the user is member of a group that is allowed access.

A better way might be to use dynamic SQL to determine if the user really
does have access. You can do this using the PERMISSIONS function. That
function only works on objects in the current database, so you need to
use dynamic SQL to check. If the user doesn't even have rights to use
that database, this probably won't work. See BOL for values for update
and delete access. I only check for insert access to the table (value of
8 in the select).

Declare @p int
Declare @nvcPermCheck nvarchar(1000)
Set @nvcPermCheck = N'Use [DB2] Select @p =
(PERMISSIONS(object_id(''Table2'')) & 8)'
Exec sp_executesql @nvcPermCheck, N'@p int OUTPUT', @p OUTPUT
If @p > 0
  Print 'has insert access'
Else
  Print 'does not have insert access'

-- 
David Gugick
Imceda Software
www.imceda.com 


Relevant Pages

  • Re: Update trigger
    ... nvltrigger for each column for example. ... SQL> drop trigger emp_upd_trig ... SQL> create table emp ( ... Commit complete. ...
    (comp.databases.oracle.server)
  • Re: Update trigger
    ... nvltrigger for each column for example. ... record update even if we are only effectively changing one column ... SQL> create or replace trigger emp_upd_trig ... SQL> update emp ...
    (comp.databases.oracle.server)
  • Re: Active directory update
    ... I would look into WMI and sp_OA* if you really want to do it from Sql. ... Modifying AD is quite serious. ... > Directory when Employee details are changed in our SQL Server database. ... > especially as there appears to be no trigger type event in AD. ...
    (microsoft.public.sqlserver.programming)
  • Re: application roles
    ... I 've made a trigger on a SQL2000 database to test your solution with the ... But my login-account and database user appear in my logtable instead of the ... if you are on SQL 2005. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (microsoft.public.sqlserver.security)
  • Re: Update trigger
    ... nvltrigger for each column for example. ... SQL> create or replace trigger emp_upd_trig ... after update of ename, job, sal on emp ... from emp where empno> 8000; ...
    (comp.databases.oracle.server)

Loading