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
Related
I want to know who created a database in sql server. Not the owner user, but the HOST_NAME or the machine name of the user who creates the database. Someone created a database on the server, but we do not know who it was. The only data we have is the user owner of the database, but that user is used by any developer. But what I want is to be able to identify the name of the machine from where the database is created. For example the host name of creation.
I tried with
select * from sys.databases
but it doesn't I need.
Then I tried with
Select
database_name,server_Name,Machine_Name,b.[name]as Backup_Name,Backup_Start_Date,Backup_Finish_Date
,Physical_Device_Name
from master.sys.databases a
Inner Join msdb..backupset
b on
a.Name = b.Database_Name
Inner Join msdb..backupmediafamily
c on c.media_set_id
= b.media_set_id
WHERE database_name = 'database'
But I also do not get the data I need
I'd use extended events for this:
CREATE EVENT SESSION [Create Database] ON SERVER
ADD EVENT sqlserver.database_created(
ACTION(sqlserver.client_hostname))
ADD TARGET package0.ring_buffer;
GO
ALTER EVENT SESSION [Create Database] ON SERVER STATE = START;
I leave the consumption of the target data as an exercise for the reader.
To Get Host Name
`SELECT HOST_NAME() AS HostName'
To Get Machine Name
SELECT serverproperty('Machinename') as Hostname
Note:
Host_Name means the sql server to which database is connected
Machinename means the machine name in which sql server is installed
I have two SQL Server database servers in two different server computers.
Server A - 192.168.1.100
Server B - 192.168.2.102
I need to execute a query from server A to retrieve data from a table in the Server B.
How to write the SQL select statement to perform this?
Are there any server configurations to allow these type of querying?
Add a linked server here are commands for 1 way of doing this. replace the user and password values with an appropriate SQL credential.
EXECUTE master.dbo.sp_addlinkedserver #server = N'192.168.2.102', #srvproduct=N'SQL Server'
EXECUTE master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'192.168.2.102',#useself=N'False',#rmtuser='ASQLLogin',#rmtpassword='Password'
then simply query like you would a normal table but append the linked server in front as commented above linked_server.db_name.schema_name.table_name like so:
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName]
You can even join it to your local server A if you want.
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName] b
INNER JOIN SomeTableOnServerA a
ON b.ID = a.ID
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, 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 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.