Sql server 如何从SQL Server数据库获取客户端IP地址
假设我有三台机器。一个是服务器,另外两个是客户端。服务器上安装了SQL数据库,另外两台计算机上安装了SQLServer客户端。因此,人们可以从客户机连接到SQLServer数据库 现在我想,每当有人对表执行DML操作时,就会触发一个触发器,从该触发器中,用户的客户机IP地址将存储在另一个表中。所以我搜索good并找到了一个脚本,它是:Sql server 如何从SQL Server数据库获取客户端IP地址,sql-server,Sql Server,假设我有三台机器。一个是服务器,另外两个是客户端。服务器上安装了SQL数据库,另外两台计算机上安装了SQLServer客户端。因此,人们可以从客户机连接到SQLServer数据库 现在我想,每当有人对表执行DML操作时,就会触发一个触发器,从该触发器中,用户的客户机IP地址将存储在另一个表中。所以我搜索good并找到了一个脚本,它是: create Procedure sp_get_ip_address (@ip varchar(40) out) as begin Declare @ip
create Procedure sp_get_ip_address (@ip varchar(40) out)
as
begin
Declare @ipLine varchar(200)
Declare @pos int
set nocount on
set @ip = NULL
Create table #temp (ipLine varchar(200))
Insert #temp exec master..xp_cmdshell 'ipconfig'
select @ipLine = ipLine
from #temp
where upper (ipLine) like '%IP ADDRESS%'
if (isnull (@ipLine,'***') != '***')
begin
set @pos = CharIndex (':',@ipLine,1);
set @ip = rtrim(ltrim(substring (@ipLine ,
@pos + 1 ,
len (@ipLine) - @pos)))
end
drop table #temp
set nocount off
end
go
declare @ip varchar(40)
exec sp_get_ip_address @ip out
print @ip
但上面的脚本提供了安装数据库的服务器IP地址。我不是在找这个剧本。我有另一个剧本,效果很好。剧本是:
CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(255)
AS
BEGIN
DECLARE @IP_Address varchar(255);
SELECT @IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID;
Return @IP_Address;
END
SELECT dbo.GetCurrentIP()
更多脚本
-*********************************************
-- DROP trigger and/or table if they exist
IF EXISTS(SELECT 1 FROM sysobjects WHERE type = 'TR' AND name = 'myTable_InsertUpdate') BEGIN
DROP TRIGGER myTable_InsertUpdate
END
IF EXISTS(SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'myTable') BEGIN
DROP TABLE myTable
END
-- Create out table; note the audit fields
CREATE TABLE myTable (
id int PRIMARY KEY NOT NULL IDENTITY(1,1)
, field1 char(1)
, changed_by_ip char(15)
, changed_by_host char(15)
, datetime_changed datetime
)
GO
-- Create trigger for update and insert
CREATE TRIGGER myTable_InsertUpdate
ON myTable
FOR insert, update
AS
DECLARE @ipLine varchar(255)
DECLARE @pos int
DECLARE @ip char(15)
-- Temporary table creation
CREATE TABLE #ip (
ipLine varchar(255)
)
-- Insert the return of ipconfig into the temp table
INSERT #ip EXEC master..xp_cmdshell 'ipconfig'
-- Find the line which contains the IP address and assign it to a variable
SET @ipLine = (
SELECT ipLine
FROM #ip
WHERE ipLine LIKE '%IP Address%'
)
-- If the IP address is known
IF Coalesce(@ipLine, '***') <> '***' BEGIN
--Find the index of the colon from the END of the string
SET @pos = CharIndex(':', Reverse(@ipLine), 1) - 1
--Trim the IP off the end of the string
SET @ip = Right(@ipLine, @pos)
--Remove any trailing or leading white space
SET @ip = RTrim(LTrim(@ip))
END
-- Drop the temp table
DROP TABLE #ip
-- Update the audit fields based on the value being updated
UPDATE myTable
SET changed_by_ip = @ip
, datetime_changed = GetDate()
, changed_by_host = host_name()
WHERE id IN (SELECT id FROM inserted)
GO
-- Insert some test values
INSERT INTO myTable (field1) VALUES ('a')
INSERT INTO myTable (field1) VALUES ('a')
-- Display initial values
SELECT * FROM myTable
-- Update one of the fields
UPDATE myTable
SET field1 = 'b'
WHERE id = 2
-- Display changed values.
SELECT * FROM myTable
-- Notice the change in datetime_changed where id = 2
GO
-- And finally; clean up after ourselves
DROP TRIGGER myTable_InsertUpdate
DROP TABLE myTable
这适用于我安装的和
我可以从触发器调用这个GetCurrentIP函数吗
GetCurrentIP函数是否与所有SQLServer版本兼容?例如,SQLServer2000
如果任何用户连接到数据库并通过第三方应用程序(如自定义应用程序)执行DML操作,此脚本能否获取客户端IP地址
大问题:但答案很短 我可以从触发器调用这个GetCurrentIP函数吗? 对。您可以在触发器中直接使用其中的代码,而不是此函数 GetCurrentIP函数是否与所有SQL Server版本兼容?例如,SQLServer2000? 否。仅适用于SQL Server 2005及更高版本。因为sys.dm_exec_会话在SQL Server 2000中不可用。对于SQL Server,您必须采用一些不同的技术 如果任何用户连接到数据库并通过第三方应用程序(如自定义应用程序)执行DML操作,此脚本能否获取客户端IP地址?
对。客户机应用程序是客户机应用程序,无论是第三方应用程序还是自制应用程序:SQL Server都无法区分。您能告诉我在SQL Server 2000中使用客户机IP的诀窍吗?因为您说SQL Server 2000中不提供sys.dm_exec_会话。如果您给我一个在SQLServer2000中保存客户端IP的示例脚本,那将非常有帮助。谢天谢地,这有点棘手。我现在没有访问SQL Server 2000的权限,但您可以尝试这些链接,看看它们是否有用:并再次帮助我了解如何像IP地址一样存储计算机名。我得到的脚本给了我客户端IP,但我也需要客户端机器名。你能帮忙吗。谢谢你和sp会帮你找到主机名。我还有一个问题。假设我通过印度办公室的远程桌面查看器连接到英国办公室的一台机器。在这种情况下,请告诉我哪个IP将存储在数据库中。我的印度办公室pc或英国机器ip。我猜英国机器ip……我说得对吗?
-- DROP trigger and/or table if they exist
IF EXISTS(SELECT 1 FROM sysobjects WHERE type = 'TR' AND name = 'myTable_InsertUpdate') BEGIN
DROP TRIGGER myTable_InsertUpdate
END
IF EXISTS(SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'myTable') BEGIN
DROP TABLE myTable
END
-- Create out table; note the audit fields
CREATE TABLE myTable (
id int PRIMARY KEY NOT NULL IDENTITY(1,1)
, field1 char(1)
, changed_by_ip char(15)
, changed_by_host char(15)
, datetime_changed datetime
)
GO
-- Create trigger for update and insert
CREATE TRIGGER myTable_InsertUpdate
ON myTable
FOR insert, update
AS
DECLARE @ipLine varchar(255)
DECLARE @pos int
DECLARE @ip char(15)
-- Temporary table creation
CREATE TABLE #ip (
ipLine varchar(255)
)
-- Insert the return of ipconfig into the temp table
INSERT #ip EXEC master..xp_cmdshell 'ipconfig'
-- Find the line which contains the IP address and assign it to a variable
SET @ipLine = (
SELECT ipLine
FROM #ip
WHERE ipLine LIKE '%IP Address%'
)
-- If the IP address is known
IF Coalesce(@ipLine, '***') <> '***' BEGIN
--Find the index of the colon from the END of the string
SET @pos = CharIndex(':', Reverse(@ipLine), 1) - 1
--Trim the IP off the end of the string
SET @ip = Right(@ipLine, @pos)
--Remove any trailing or leading white space
SET @ip = RTrim(LTrim(@ip))
END
-- Drop the temp table
DROP TABLE #ip
-- Update the audit fields based on the value being updated
UPDATE myTable
SET changed_by_ip = @ip
, datetime_changed = GetDate()
, changed_by_host = host_name()
WHERE id IN (SELECT id FROM inserted)
GO
-- Insert some test values
INSERT INTO myTable (field1) VALUES ('a')
INSERT INTO myTable (field1) VALUES ('a')
-- Display initial values
SELECT * FROM myTable
-- Update one of the fields
UPDATE myTable
SET field1 = 'b'
WHERE id = 2
-- Display changed values.
SELECT * FROM myTable
-- Notice the change in datetime_changed where id = 2
GO
-- And finally; clean up after ourselves
DROP TRIGGER myTable_InsertUpdate
DROP TABLE myTable