0

我的 SQL 脚本是从模板生成的。它们包含 IP 地址。它们可能很多,操作员可能会错误地执行不正确的脚本。脚本如何检查它是否在正确的机器上启动?(否则,我想打印消息并退出。)

有可能吗?

谢谢,彼得

4

4 回答 4

3

更新的答案

这是我从http://weblogs.sqlteam.com/peterl/archive/2008/07/16/How-to-get-IP-address.aspx找到的脚本,并进行了一些修改以更好地满足您的需求:

DECLARE @TargetIpAddress varchar(15);
SET @TargetIpAddress = '127.0.0.1'; --<== The IP address of the server you want.

DECLARE @Interfaces TABLE
(
    RowID int IDENTITY(0, 1)
   ,Interface char(38)
   ,IP varchar(15)
);

INSERT @Interfaces ( Interface )
  EXEC master..xp_regenumkeys N'HKEY_LOCAL_MACHINE',
       N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces';

DECLARE @RowID int
       ,@IP varchar(15)
       ,@Key nvarchar(200);

SELECT @RowID = MAX(RowID)
  FROM @Interfaces;

WHILE @RowID >= 0
BEGIN
    SELECT @Key = N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces\' + Interface
     FROM @Interfaces
    WHERE RowID = @RowID;

    EXEC master..xp_regread N'HKEY_LOCAL_MACHINE', @Key, N'DhcpIPAddress', @IP OUTPUT;

    IF @IP <> '0.0.0.0'
    UPDATE @Interfaces
       SET IP = @IP
     WHERE RowID = @RowID;

    SET @RowID = @RowID - 1;
END;

IF NOT EXISTS (SELECT IP FROM @Interfaces WHERE IP = @TargetIpAddress)
BEGIN
    DECLARE @ErrorMessage varchar(2000);
    SET @ErrorMessage  = 'This is not the correct server. This server does not have an IP address of %s.';
    SET @TargetIpAddress = ISNULL(@TargetIpAddress, 'NULL');
    RAISERROR(@ErrorMessage, 16, 1, @TargetIpAddress);
END

-- The rest of the script...

原始答案

似乎使用 Server\Instance 名称而不是 IP 地址会更容易使用,并且如果服务器在以后分配了不同的 IP 地址,则不太可能中断。

-- You can get the instance name like this:
SELECT @@SERVERNAME +'\'+ @@SERVICENAME AS 'Instance';

-- Although, you might prefer this instead:
SELECT CAST(SERVERPROPERTY('MachineName') AS nvarchar(128))
      +COALESCE('\'+CAST(SERVERPROPERTY('InstanceName') AS nvarchar(128)), '');

-- NetBIOS name of the local computer on which the instance of SQL Server
-- is currently running.
-- If the instance of SQL Server is in a failover cluster and you want to obtain
-- the name of the failover clustered instance, use the MachineName property. 
SELECT SERVERPROPERTY('ComputerNamePhysicalNetBIOS');

您可以在 MSDN 中找到有关 SERVERPROPERTY 函数的详细信息:SERVERPROPERTY (Transact-SQL)。虽然,此函数不提供任何方式来获取服务器/实例的 IP 地址 - 没有提供此信息的内置函数。

于 2012-11-04T07:19:48.083 回答
1

很抱歉重新发布,但对总是错过访问 SQL-Server 内的 systemadata 的机会感到恼火,我决定编写一个扩展存储过程作为 WMI 的桥梁。

下载http://bummisoft.de/download/XP_WMI.zip

电话是:

exec xp_wmiv3 'Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE'

该 DLL 可用于 32 位和 64 位 SQLServer,并且可免费用于非商业用途。

安装:

EXEC sp_addextendedproc xp_wmiv3, '<Your Path>\XP_WMIV3_DLL.dll'

例如: EXEC sp_addextendedproc xp_wmiv3, 'C:\DLLs\XP_WMIV3_DLL.dll'

卸载

EXEC sp_dropextendedproc xp_wmiv3

用法

例如

exec xp_wmiv3 'SELECT * FROM Win32_Volume'
exec xp_wmiv3 'SELECT * FROM CIM_Userdevice where Name like "%HID%"'



create table #tmp(
    Domain varchar(255),
    Name varchar(255),Sid varchar(255)
)
insert into #tmp 
exec xp_wmiV2 'SELECT Domain, SID, Name FROM Win32_UserAccount where Status = "OK"'
select * from #tmp
drop table #tmp

目前在 SQL-Server 2005 和 SQL-Server 2008 下测试。

于 2012-11-09T22:42:55.623 回答
1

我可以提供一个 xp_GetIP.dll,但仅使用 SQL-Server 2005 32 位进行测试,您可以通过它注册

EXEC sp_addextendedproc xp_GetIP, 'C:\temp\xpGetIP.dll'

电话将是

Declare @IP varchar(100)

exec xp_GetIP @IP output
print @IP

仅输出:192.168.69.69

编辑:使用 SQL-Server 2008 R2 64 位 als 64 位 DLL

于 2012-11-05T13:25:51.417 回答
1
Create Procedure P_GetIPAddresses (@IPS varchar(4000) out)
as
begin
Select @IPS=''
Create TABLE #temp (Line varchar(200))
Insert #temp exec master..xp_cmdshell 'ipconfig'
Select @IPS = @IPS + Coalesce(RTRIM(Replace(SubString(Line,1,CharIndex(':',line)-1)  ,'.','')) + SubString(Line,CharIndex(':',line) ,200),'')
from #temp
where upper (Line) like '%ADRESS%'
--SELECT * from #tmp --DEBUG
DROP TABLE #temp

end 

如果 xp_cmdshell 被禁用,则解析地址由您决定...

EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO


Declare @IPS Varchar(4000)

exec P_GetIPAddresses @IPS out
Select  @IPS

EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 0
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
于 2012-11-05T12:02:59.430 回答