我的SQL脚本是从模板生成的。它们包含IP地址。可能有很多这些,操作员可能会错误地执行不正确的脚本。脚本如何检查它是否在正确的机器上启动? (否则,我想打印消息并退出。)
有可能吗?
谢谢, 彼得
答案 0 :(得分: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 (Transact-SQL)找到有关SERVERPROPERTY功能的详细信息。虽然,此函数不提供任何获取服务器/实例的IP地址的方法 - 但没有提供此信息的内置函数。
答案 1 :(得分: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
答案 2 :(得分:1)
我可以提供xp_GetIP.dll,但仅使用32位SQL-Server 2005进行测试 您可以通过
注册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
答案 3 :(得分: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位SQL Server,并且可以免费用于非商业用途。
安装:
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
用法
e.g。
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下进行了测试。