Click here to Skip to main content
15,914,924 members
Home / Discussions / Database
   

Database

 
QuestionSQL Server : Add user login in a trigger? Pin
Duncan Edwards Jones20-Apr-05 0:12
professionalDuncan Edwards Jones20-Apr-05 0:12 
OK - I have a database with a table [User permissions] which controls what the user of the print monitoring application is allowed to do (i.e. see only own documewnts, or all etc.)

When a user submits a print job their name is passed to the database by the print monitoring service. If they have not been added to this table then they are added with the minimum permissions. I would like this step to also set up their database log in so have set a trigger thus:-

<br />
/* ---------------------------------------------<br />
 * Add an NT login to this database from the <br />
 * NT Domain.<br />
 */<br />
CREATE TRIGGER User_Permissions_Insert<br />
  ON dbo.[User Permissions]<br />
 FOR INSERT<br />
AS<br />
	<br />
	DECLARE @DOMAIN VARCHAR(200),<br />
	        @FULL_USERNAME VARCHAR(255)<br />
	<br />
	<br />
       SELECT @DOMAIN = ISNULL([Parameter Value],'')<br />
	  FROM dbo.[PUMA Parameters]<br />
	 WHERE [Parameter Name] = 'NT Domain'<br />
	<br />
	IF @DOMAIN = ''<br />
	   SELECT @FULL_USERNAME = [User Name]<br />
             FROM inserted<br />
	ELSE<br />
	   SELECT @FULL_USERNAME = LTRIM(@domain) + '\' + Ltrim([User Name])<br />
             FROM inserted<br />
<br />
<br />
        -- Add the NT login to the database logins list<br />
        EXECUTE sp_grantlogin @FULL_USERNAME<br />
<br />
        -- And grant them access to the current database<br />
        EXECUTE sp_grantdbaccess @FULL_USERNAME<br />
<br />
<br />
	RETURN 


However when I cause the trigger to fire I get the error message:-
The procedure 'sp_grantlogin' cannot be executed within a transaction.

Any ideas how I would go about doing this, or is a trigger the wrong solution for this case?


'--8<------------------------
Ex Datis:
Duncan Jones
Merrion Computing Ltd
AnswerRe: SQL Server : Add user login in a trigger? Pin
Mike Dimmick20-Apr-05 1:23
Mike Dimmick20-Apr-05 1:23 
GeneralRe: SQL Server : Add user login in a trigger? Pin
Duncan Edwards Jones20-Apr-05 2:16
professionalDuncan Edwards Jones20-Apr-05 2:16 
Generalauotmatically generating a unique ID number [* Resolved *] Pin
steve_rm19-Apr-05 21:50
steve_rm19-Apr-05 21:50 
GeneralRe: auotmatically generating a unique ID number Pin
Mike Dimmick19-Apr-05 23:25
Mike Dimmick19-Apr-05 23:25 
GeneralRe: auotmatically generating a unique ID number Pin
totig22-Apr-05 2:20
totig22-Apr-05 2:20 
GeneralRe: auotmatically generating a unique ID number Pin
steve_rm22-Apr-05 13:27
steve_rm22-Apr-05 13:27 
Generalhelp - newbie needs help with sql problem Pin
squeakerz19-Apr-05 16:22
squeakerz19-Apr-05 16:22 
GeneralRe: help - newbie needs help with sql problem Pin
Christian Graus19-Apr-05 17:46
protectorChristian Graus19-Apr-05 17:46 
GeneralRe: help - newbie needs help with sql problem Pin
squeakerz20-Apr-05 1:37
squeakerz20-Apr-05 1:37 
GeneralRe: help - newbie needs help with sql problem Pin
Colin Angus Mackay20-Apr-05 2:06
Colin Angus Mackay20-Apr-05 2:06 
GeneralRe: help - newbie needs help with sql problem Pin
squeakerz20-Apr-05 3:42
squeakerz20-Apr-05 3:42 
GeneralRe: help - newbie needs help with sql problem Pin
Colin Angus Mackay20-Apr-05 3:49
Colin Angus Mackay20-Apr-05 3:49 
GeneralRe: help - newbie needs help with sql problem Pin
squeakerz20-Apr-05 8:05
squeakerz20-Apr-05 8:05 
GeneralSearch - Question Pin
matthias s.19-Apr-05 1:04
matthias s.19-Apr-05 1:04 
GeneralRe: Search - Question Pin
totig22-Apr-05 2:22
totig22-Apr-05 2:22 
GeneralRe: Search - Question Pin
Anonymous23-Apr-05 16:31
Anonymous23-Apr-05 16:31 
QuestionNHibernate configuration file - multiple datasource? Pin
devvvy19-Apr-05 0:20
devvvy19-Apr-05 0:20 
GeneralReporting Service Pin
sukasukabo18-Apr-05 22:39
sukasukabo18-Apr-05 22:39 
GeneralRe: Reporting Service Pin
NewSilence23-Apr-05 13:28
NewSilence23-Apr-05 13:28 
Generalolap Pin
wild_daisy18-Apr-05 11:13
wild_daisy18-Apr-05 11:13 
GeneralSELECT MIN Pin
Jaydeanster18-Apr-05 9:05
Jaydeanster18-Apr-05 9:05 
GeneralRe: SELECT MIN Pin
Colin Angus Mackay18-Apr-05 10:19
Colin Angus Mackay18-Apr-05 10:19 
Generalrdl query Pin
tvprithiv18-Apr-05 1:48
tvprithiv18-Apr-05 1:48 
GeneralRe: rdl query Pin
NewSilence23-Apr-05 13:22
NewSilence23-Apr-05 13:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.