SQL Query for Logins - sql

What is the SQL query to select all of the MSSQL Server's logins?
Thank you. More than one of you had the answer I was looking for:
SELECT * FROM syslogins

Is this what you're after?
select * from master.syslogins

On SQL Azure as of 2012;
logins:
--connecct to master
--logins
SELECT * from sys.sql_logins
--users
SELECT * from sys.sysusers
and users on a specific database:
--connect to database
SELECT * from sys.sysusers
Also note that 'users' on Azure SQL now (2022-11-17) have more 'login' type properties and creating a user on a Azure SQL database with a password is now possible, so it is less likely to require creating logins in 'master'.

EXEC sp_helplogins
You can also pass an "#LoginNamePattern" parameter to get information about a specific login:
EXEC sp_helplogins #LoginNamePattern='fred'

Starting with SQL 2008, you should use sys.server_principals instead of sys.syslogins, which has been deprecated.

#allain, #GateKiller your query selects users not logins
To select logins you can use this query:
SELECT name FROM master..sysxlogins WHERE sid IS NOT NULL
In MSSQL2005/2008 syslogins table is used insted of sysxlogins

Selecting from sysusers will get you information about users on the selected database, not logins on the server.

sp_helplogins will give you the logins along with the DBs and the rights on them.

Select * From Master..SysUsers Where IsSqlUser = 1

Have a look in the syslogins or sysusers tables in the master schema. Not sure if this still still around in more recent MSSQL versions though. In MSSQL 2005 there are views called sys.syslogins and sys.sysusers.

Related

How to find databases associated based on server credentials in sql server

I want to find the associated Databases of a particular user based on Server authentication credentials in SQL server.
as per my understading you want this .it will give list of databases based on the username using sys.databases
SELECT name FROM sys.databases where suser_sname( owner_sid ) = 'trainee' order by name
you should pass username in place of trainee
Once you connect to SQL Server using provides UserName/Password, You can query sys.databases view:
SELECT [DatabaseName] = name
FROM [sys].[databases]
WHERE HAS_DBACCESS([name]) = 1

Roles in SQL 2008 R2

When I’m trying to create a role then I receive the error “role exists” and I have to deleted first and repeat the process.
How may I check if a role exists in my sql database in sql server 2008 r2?
Is there any sys.table to search for it?
use sys.database_principals view:
select * from sys.database_principals where name = #Role_Name and type = 'R'
it's also possible to use database_principal_id:
select database_principal_id(#Role_Name)

In SQL Server, why there are duplicated DMVs in all database? Such as sys.databases

In SQL Server, there are some DMVs that appear in all databases and have the same content in it. What's the purpose of this approach? For example, the following 2 query will give the same result.
select *
from master.sys.databases
select *
from tempdb.sys.databases
Thanks.
sys.databases only exists once (actually in the hidden resource database)
All you are doing above is switching db context temporarily by using a 3 part object name

What is the best way to check for the existence of a Login in SQL Server 2005

I am looking for the best way to check that a database login exists in SQL Server 2005. I am currently using
IF suser_sid('loginname') IS NOT NULL
but suser_sid() returns a value in some cases where a login does not exist.
In SQL 2000 we use
SELECT * FROM [Master].[dbo].[sysxlogins] WHERE [name] ='loginname'
but that table does not exist in SQL 2005.
There is a similar question about checking the existence of Users, which is helpful, but I am looking for the existence of Logins.
For sql2005...
select * from master.sys.syslogins WHERE [name] ='loginname'
syslogins is not recommended by Microsoft. It's better to use sys.server_principals.
select * from sys.server_principals where name = 'login_name'

How to check a SQL Server CE database for indexes?

Is there a way to list all SQL Server CE database table indexes, or at least for individual tables?
-- Retrieves information about the indexes contained in the database.
SELECT * FROM
INFORMATION_SCHEMA.INDEXES
-- Retrieves all the Tables in the database including the System tables.
SELECT * FROM
INFORMATION_SCHEMA.TABLES
Arjuna Chiththananda - Retrieving Schema Information of SQL CE Database
Thank you Arjuna! you pointed me in the right direction..... The following works.
SELECT 'Y' FROM INFORMATION_SCHEMA.INDEXES
where table_name = 'PP_JOB_TICKET_LIVE'
and index_name = 'PP_JOB_TICKET_LIVE_PK'
Thanks so much for your time.