I made a function that takes a varchar
(IP address) and returns table values (city, country, time_zone...)
ALTER function [dbo].[GQuery2](@ipAddress varchar(16))
returns @t table (
[country_name] [nvarchar](64) ,
[region_name] [nvarchar](128) ,
[city_name] [nvarchar](128),
[latitude] [float],
[longitude] [float] ,
[zip_code] [nvarchar](30) ,
[time_zone] [nvarchar](8)
)
as
begin
insert into @t ([country_name]
,[region_name]
,[city_name]
,[latitude]
,[longitude]
,[zip_code]
,[time_zone])
select [country_name]
,[region_name]
,[city_name]
,[latitude]
,[longitude]
,[zip_code]
,[time_zone]
from [pcap].[dbo].ip2loc
where ip_from = (
select max(ip_from)
from [pcap].[dbo].ip2loc
where ip_from <= [dbo].IP2INT(@ipAddress)
)
and ip_to = (
select min(ip_to)
from [pcap].[dbo].ip2loc
where ip_to >= [dbo].IP2INT(@ipAddress)
)
RETURN
end
The problem is I can only call the function in this format:
SELECT * FROM [dbo].GQuery2('89.98.0.0')
and it returns a new table
country_name region_name city_name latitude longitude zip_code time_zone
NETHERLANDS NOORD-HOLLAND AMSTERDAM 52.37403 4.88969 1089 +02:00
And I want to make a query like this (theoretically)
UPDATE DimLocation d, [dbo].GQuery2(d.ip) g
SET d.city_name = g.city_name, d.country_name = g.country_name ....
The DimLocation
table has the schema:
IP, country_name, region_name, city_name, latitude, longitude, zip_code, time_zone
The IP address has a value and the other columns are NULL and i need to pass the IP address from DimLocation
to the function GQuery2(ip)
to get the missing data
The question is how to work with table-valued functions in select
statements?