условно создайте пользователя в SQL Server

Это можно сделать с помощью cross apply, чтобы устранить необходимость знать возможные значения в fieldTwo:

select twos.FieldTwo, count(1)
from (select distinct fieldTwo from MyTable) twos
  cross apply (select distinct t.fieldOne
               from MyTable t
               where t.fieldTwo = twos.FieldTwo) ones
group by twos.FieldTwo
23
задан marc_s 6 January 2010 в 15:49
поделиться

2 ответа

You can consult the two system catalogs sys.server_principals to check for server logins, or sys.database_principals in your specific database for users of your database:

use myDB
GO

if not exists(select * from sys.database_principals where name = 'foo')
  -- create your database user


if not exists(select * from sys.server_principals where name = 'foo')
   -- you need to create a server login first

Marc

32
ответ дан 29 November 2019 в 01:10
поделиться

Вот что мы делаем ...

IF NOT EXISTS (SELECT * FROM DBO.SYSUSERS WHERE NAME = @usrName )
BEGIN
  PRINT 'Granting access to the current database to login ' + @usrName + '...'
  -- Grant access to our database
  EXEC SP_GRANTDBACCESS @usrName
END ELSE BEGIN  
  PRINT 'Login ' + @usrName + ' already granted access to current database.'  
END  
14
ответ дан 29 November 2019 в 01:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: