How to Know who creates (host name) a database in sql server? - sql

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

Related

SQL Server Querying Online

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

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)

How to update a table taking values to a second sql instance?

I have two SQL Server instances on two virtual machines, Test server and Production server.
I need to do an UPDATE to the production server taking data values from test server.
For example :
UPDATE [server_production].dbname.mytable
SET column1 = [server_test].dbname.mytable.column1
How do I do this ?
On your production server, define a Linked Server that points to your test server.
Then, on your production server you can run a query similar to the following to update your column:
UPDATE p
SET column1 = t.column1
from <dbname>.<schema>.mytable p
join <TestLinkedServerName>.<dbname>.<schema>.mytable t
on p.<id> = t.<id>
In the above query, you'll need to provide values for the placeholders:
<dbname> - the name of the prod/test database
<schema> - the schema in which the table is defined (typically dbo)
<TestLinkedServerName> - the name you've given to the linked server
<id> - your PK column, or a column that uniquely identifies rows and provides a way to join the two tables
Have a look at redgate sql data compare
http://www.red-gate.com/products/sql-development/sql-data-compare/

sql query to select a value from one database

Hai,
I have two database and I want to select one value from one of the databases....
for that I want to pass one value and if that value is stored in the database I want to pick the id representing the value in the database.
that means the operation is that.....
first I select a row of data from one database by using a user control...
in that row there is a value (example "apple") and I want pass this value("apple") to the second database... in the second database the value("apple") having a id (example "australian") I want that the query search for that id("australian") and show that in the text box.
Please help me....
thanks to all advance....
example
first database
id name details
1 apple sweet
2 orange sweet
second database
id name details
Australian apple sold
Indian banana sold
Imagine that there are the two databases....
using a user control I select first row from first database and I want to pass that value apple to second database and find out the id australian from the second database and show that in a text box....
thank you.........
You can do a join between the two databases as long as you use the fully qualified prefix for each one.
I think you should go for the join , your query should look something like this
SELECT SecondDataBase.TableName.Id
FROM FirstDatabase.TableName INNER JOIN
SecondDataBase.TableName ON FirstDatabase.TableName.["Column contains Apple"] = SecondDataBase.TableName.["Column contains Apple"]
Fully qualified table name, which includes server name, db name, schema and table (e.g. MySqlServerInstance1.mydb1.dbo.table1) name will definitely work as long as one database server has a registered reference within a calling DB server. See this for things you have to do if you are using MS SQL Server: http://msdn.microsoft.com/en-us/library/ms188231.aspx
In MSSQL you normally reference a table using SchemaName.TableName
dbo.Fruit
The database is automatically determined by your connection string. FirstDatabase
So when you use dbo.fruit, the server automatically appends the database name to the table like
FirstDatabase.dbo.Fruit
If the user account has permission, you can select from a completely different database by specifying the database
SecondDatabase.dbo.FruitSales
To take it even further you can select from an entirely different SQL server if you have set up a linked server by specifying the linked server name like
SecondServer.ThirdDatabase.dbo.FruitShipping
So you can join between a table in your database and a table in your second database like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondDatabase.dbo.FruitSales AS FS ON F.Something = FS.Something
But you can even join between a table in your database and a table on a different server like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondServer.ThirdDatabase.dbo.FruitShipping AS FS ON F.Something = FS.Something