I have the exact Query Text and the exact time & date the Query was executed,
how can I find the Host Name that executed this query?
I'm using SQL Server 2008.
Do you need ##SERVERNAME
SELECT ##SERVERNAME
will return the server name where the query was executed.
HOST_NAME will return the workstation name
SELECT HOST_NAME()
There is not any table with historical information about the host that executed a query - http://www.sqlservercentral.com/Forums/Topic1334479-146-1.aspx
You can check with HostName() as
SELECT HOST_NAME() AS HostName, SUSER_NAME() LoggedInUser
http://blog.sqlauthority.com/2009/05/26/sql-server-find-hostname-and-current-logged-in-user-name/
or
SELECT #IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = ##SPID;
How to get the client IP address from SQL Server 2008 itself?
How to identify the caller of a Stored Procedure from within the Sproc
This is as close as you'll get, I believe:
select host_name()
As is mentioned in the docs: "The client application provides the workstation name and can provide inaccurate data. Do not rely upon HOST_NAME as a security feature."
Related
Suppose I have two database on two separate servers:
server1:
ip 127.0.0.1
user1, passwd1
db1.tbl1
server2:
ip 127.0.0.2
user2, passwd2
db2.tbl2
Suppose tb1 and tb2 has the same table structure. I've search the web, people suggest to set up linked-servers, but I don't want to set up.
Since I know the password and ip address, things should go very easy like this:
insert into db2.tbl2
select *
from db1.tbl1
However I don't know where to put the ip address and username and password.
Try this (run it on server1):
INSERT INTO [db1].[tbl1]
SELECT * FROM OPENDATASOURCE(
'SQLOLEDB',
'Data Source=127.0.0.2;Initial Catalog=db2;Persist Security Info=True;User ID=user2;Password=passwd2'
).db2.tbl2
You can refer to this article.
You tagged your question with many RDBMS.
My answer is about SQL Server.
Since I know the password and ip address, things should go very easy
like this: insert into db2.tbl2 select * from db1.tbl1
And this is exactly the syntax you can use in case of linked servers. You incapsulate connection string in the definition of linked server, then the only thing that changes is linked server name in your code:
insert into db2.tbl2 select * from server1.db1.tbl1
example of linked server setup:
EXEC master.dbo.sp_addlinkedserver #server = N's_2005', #srvproduct=N'', #provider=N'SQLNCLI', #datasrc=N'127.0.0.1,8851'
go
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N's_2005',#useself=N'False',#locallogin=NULL,#rmtuser=N'login1',#rmtpassword='*****'
go
select top 5 * from [s_2005].[Northwind].[dbo].[Employees]
If you want other solutions, you can use OPENROWSET (Transact-SQL)
providing the whole connection string in your INSERT every time you want to access remote server:
select a.* from openrowset('SQLNCLI', 'Server=127.0.0.1,8851;Trusted_Connection=no;uid=login1;pwd=*****;', 'select top 5 * from [Northwind].[dbo].[Employees]') as a
The third option is SSIS where you incapsulate connection strings into SourceConnection and DestinationConnection
Here is a picture of the same result using linked server and openrowset:
If you are using SQL Server Management Studio then there is a wizard to import/export data between servers and databases:
https://learn.microsoft.com/en-us/sql/integration-services/import-export-data/start-the-sql-server-import-and-export-wizard?view=sql-server-2017
I'm wondering if there is a SQL query that would show the IP of the client that wrote the query. Like if I have people making changes to my database I already have a log table but I want to add IP address to it. So I am wondering if this this possible.
If the connection was made with TCP, the query below will return the client IP address. Note that VIEW SERVER STATE permissions are required with this method.
SELECT client_net_address
FROM sys.dm_exec_connections
WHERE
session_id = ##SPID
AND net_transport = 'TCP';
I wanna learn remote machine's ip adressess which accessed my sql server. someone at my job(in local network) find my database password and making row updates. how could I find the access person's ip?
In SQL Server you can try this:
CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(280)
AS
BEGIN
DECLARE #IP_Address varchar(280);
SELECT #IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = ##SPID;
Return #IP_Address;
END
Add a trigger to the table that adds the server variables like host name to a separate table. See trigger on update. Also select host_name.
SQL Server Profiler shows NtUsername.
How do I see the NTusername in trigger?
I don't understand. SQL Server Profiler shows the Windows username. SUSER_NAME, SUSER_SNAME ORIGINAL_LOGIN, USER, USER_NAME shows the username of the SQL user account. I want what the Profiler is showing. There must be a way if the profiler can do it.
You use SUSER_SNAME(). Not SUSER_NAME(). Note the extra S
SUSER_SNAME returns the connection user (SQL Server 2000+)
SUSER_NAME gives the value of sys.server_principals.principal_id (SQL Server 2005+)
SUSER_NAME will give NULL for a Windows user who connects via a Windows Group, because only the Windows Group has an entry in sys.server_principals
Notes:
you have SUSER_ID and SUSER_SID at the SQL Server Instance level which are the opposite
at the database level, you have USER_NAME and DATABASE_PRINCIPAL_ID (USER_ID is deprecated)
A SQL login won't have an ntusername value in Profiler, because it isn't available
Since I could not find how to get the NTUsername, I used HOST_NAME() which gave me the user's machine name.
SUSER_NAME, SUSER_SNAME, ORIGINAL_LOGIN, USER, USER_NAME all gave me the SQL account username.
There is no correlation between the NTUserName unless the login is a Windows Authentication login. But because suser_name() is not returning what you want, I'm assuming that you are using SQL Server Authentication.
In which case, I don't believe you can do this.
It apears that the protocol used in your case was NamedPipes.
if NTUserName is not blank and in LoginName you have SQL Login – then it is SQL Authentication Over Named Pipes
-- see http://sqlbg.wordpress.com/2011/05/28/how-to-check-what-sql-server-network-protocol-is-used-by-connections/
While I'm trying to detach a database through Enterprise Manager, it shows the no. of users accessing the database and it didn't allow us to detach without clearing the database connection.
Well, I want to know whether the result (no. of users connecting a particular database) could be obtained through a SQL query? If yes, how ?
Happiness Always
BKR Sivaprakash
This will give you proper results. Add your database name in the query -
select spid, status, loginame,
hostname, blocked, db_name(dbid) as databasename, cmd
from master..sysprocesses
where db_name(dbid) like '%<database_name>%'
and spid > 50
This will include logins from SQL Agent. Note that the
same user can be using multiple connections from the same application,
and thus be counted more than once.
EXEC SP_WHO
or
EXEC SP_WHO2
maybe (think this might be SQL Server 2005 upwards):
SELECT COUNT(*) AS ConnectionCount,
CASE WHEN is_user_process =1 THEN 'UserProcess' ELSE 'System Process' END
FROM sys.dm_exec_sessions
GROUP BY is_user_process