更新带有变量的语句和来自API的答案

时间:2018-11-11 13:40:45

标签: sql-server

我正在尝试完成看似非常基本的查询,但是我迷路了。 基本上,我要在几分钟前创建的SQL表中查找条目,然后从中收集地址并放入变量。

然后我用地址查询 google.maps 并收集以公里为单位的行驶距离。

如果我一步一步地完成任务,我可以找到记录,得到google的答案(正确的答案!是的),现在的问题是将所有事情都运行在一个工作中并安排它。

以下代码可以正常工作:

DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select 
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
 from cicmpy with (nolock)
 where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+') 
 from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
 from cicmpy with (nolock) where cmp_wwn = 
(select account from projectaccounts with (nolock) where project = 
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
 from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=MYGOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)') 
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)') 
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm

现在,我尝试使其能够更新5分钟前已修改的所有条目:

Begin transaction
update absences set absences.FreeTextField_15=(
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select 
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
 from cicmpy with (nolock)
 where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+') 
 from absences ab where ab.type = '138' and hid = ab.hid)
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
 from cicmpy with (nolock) where cmp_wwn = 
(select account from projectaccounts with (nolock) where project = 
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
 from absences ab where ab.type = '138' and hid = ab.hid)
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=GOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)') 
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)') 
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
)
where absences.type = '138' and absences absences.sysmodified <(select dateadd(mi,-5,getdate()))

在PRINT @DistanceInKm用红色下划线标出后,@ DECLARE(初始字母和)附近的问题是=错误语法。

另一个问题引起了我的注意:由于更新语句是主要问题,我觉得我想念一个用来定义记录ID的where子句(在子查询中查找地址的名为 ab.hid )。 / p>

我的SQL知识是通过反复试验和逐步学习而掌握的。

感谢您的良好合作。

0 个答案:

没有答案