Это можно сделать с помощью 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
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
Вот что мы делаем ...
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