-1

我正在尝试构建一个基于在此类表中插入 ip 值的查询。它是基于 mmorpg 游戏数据库的。

所以这里是查询

declare @ip1 varchar (max)
declare @ip2 varchar (max)
declare @ip3 varchar (max)
declare @ip4 varchar (max)

set @ip1 = 'xx'--1.IP 
set @ip2 = 'xxx' -- 2.IP
set @ip3 = 'xx' -- 3.IP
set @ip4 = 'xx' --4.IP

if exists (select IP3 from _PrivilegedIP where IP3 = @ip3)
begin
   print 'Shit, this IP already exist'
end
else if exists (select IP4 from _PrivilegedIP where IP4 = @ip4)
begin
   print 'Shit, this IP already exist'
end
else
begin
     insert into _PrivilegedIP values(@ip1, @ip2, @ip3, @ip4, @ip1, @ip2, @ip3, @ip4)
     print @ip1+'.'+@ip2+'.'+@ip3+'.'+@ip4+'has been added successfully---Omg123'
end

现在,我正在尝试这样做

设置@id='xxx.xxx.xxx.xxx'

然后,它将在点之间拆分值并在表中更新为查询。

有什么帮助吗?

4

1 回答 1

2

PARSENAME()功能是您的朋友...

DECLARE @ip VARCHAR(50) = '123.124.125.126'

DECLARE @ip1 VARCHAR(10) = PARSENAME(@ip, 4) 
DECLARE @ip2 VARCHAR(10) = PARSENAME(@ip, 3)
DECLARE @ip3 VARCHAR(10) = PARSENAME(@ip, 2)
DECLARE @ip4 VARCHAR(10) = PARSENAME(@ip, 1)

IF NOT EXISTS 
(
  SELECT * 
  FROM _PrivilegedIP
  WHERE IP4 = @ip4
  OR IP3 = @ip3
)
INSERT _PrivilegedIP (IP1, IP2, IP3, IP4)
VALUES (@ip1, @ip2, @ip3, @ip4)

单击此处在 SQL Fiddle 上查看它的实际效果。

于 2013-09-14T18:08:38.263 回答