top of page

Login und Rollenzuordnung / login and add to role

(alle Versionen/all versions)

Freitag, 23. Juni 2023

Hintergrund / Background

SQL script



Hintergrund / Background


Sehr häufig muss man in der Praxis bestehende SQL-Server-Logins einer bestimmten Datenbank zuordnen und einer bestimmten Rolle in der Datenbank.

Ein häufiges Anwendungsszenario im Sage 100-Umfeld ist z.B. ein notwendiger Datenzugriff per SQL auf Ressourcen, die sich in der "OLGlobal"-Datenbank befinden, z.B. für lesende Zugriffe aus anderen Anwendungen.

Das nachfolgende Script zeigt, wie eine solche Zuordnung möglich ist.

Im Beispiel wird der Benutzername "_OLSys_TODOMyUserId" verwendet.

Das Präfix "_OLSys_" ist ein typisches Präfix für den internen Sage Benutzer.

Im vorliegenden Fall wird der Benutzer der "db_datareader" Rolle zugewiesen.


In practice, you have to assign existing SQL Server logins to a specific database and a specific role in the database.

A frequent application scenario in the Sage 100 environment is, for example, necessary data access via SQL to resources located in the "OLGlobal" database, e.g. for read access from other applications.

The following script shows how such an assignment is possible.

In the example, the username "_OLSys_TODOMyUserId" is used.

The prefix "_OLSys_" is a typical prefix for the internal Sage user.

In this use case the user is set to the role "db_datareader".


SQL script

-- TODO: Replace the database "OLGlobal" with your database !
-- TODO: Replace the user id "_OLSys_TODOMyUserId" with your user id !

USE [OLGlobal]
GO

IF NOT EXISTS (
  SELECT *
  FROM sys.database_principals
  WHERE name = '_OLSys_TODOMyUserId'
  )
BEGIN
 CREATE USER [_OLSys_TODOMyUserId]
 FOR LOGIN [_OLSys_TODOMyUserId]
END
GO

USE [OLGlobal]
GO

ALTER USER [_OLSys_TODOMyUserId]
 WITH DEFAULT_SCHEMA = [dbo]
GO

USE [OLGlobal]
GO

IF EXISTS (
  SELECT *
  FROM sys.database_principals
  WHERE name = 'db_datareader'
   AND type = 'R'
  )
BEGIN
 ALTER ROLE [db_datareader] ADD MEMBER [_OLSys_TODOMyUserId]
END
GO

bottom of page