这里理想的情况是使用基于标量的函数,获取IP地址列表,存储为varchar,转换为整数,并匹配另一个地理信息表。
问题是, 我认为包含IP的列存在问题 。如果有垃圾或特殊字符,我无法通过选择看到,只有ip存储好了。
想知道我的语法是否遗漏了什么,除了检查NULL,空字段之外还有更好的检查,我尝试过所有这些。甚至是ISNUMERIC(转换功能(ip' s)) 如果我从包含良好IP的历史表中选择一条记录,我可以转换为整数。所以有一些事情我无法看到。
有关进一步提出失败的建议,忽略任何错误的数据吗?"
下面是一个这样的MSSQL查询。
WITH CTE
AS (
--get distinct list of ips from tblHistory
SELECT DISTINCT ip
FROM tblHistory aaa WITH (NOLOCK)
--Join history to report table on an id
INNER JOIN tblReport bbb WITH (NOLOCK) ON aaa.tbl_id = bbb.tbl_id
AND aaa.inserteddate > dateadd(d, - 5, getdate())
AND ip <> ''
)
--convert distinct list and insert into temp table (bad design... but it's way faster for some reason)
--using scalar function to insert ip's converted to bigint to temp table, to match against OtherTable
SELECT dbo.IPAddressToNumber(cte.ip) ipint
,ip
INTO #temp
FROM cte
--now join against the temp table and update where the ip int matches
UPDATE aaaa
SET text_column = '[Text Comment To Add] ' + comments
,updatecolumnvalueto_one = 1
FROM tblHistory aaaa
INNER JOIN tblReport bbbb ON aaaa.tbl_id = bbbb.tbl_id
INNER JOIN #temp ON #temp.ip = bbbb.ip
INNER JOIN OtherTable c ON #temp.ipint BETWEEN c.beginnumber
AND c.endnumber
WHERE inserteddate > dateadd(d, - 5, getdate())