Update statement with variables and answer coming from an API











up vote
0
down vote

favorite












I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.



Then I query google.maps with the addresses and collect the driving distance in km.



If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.



The following code works fine:



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


Now I try to make it work to update all entries that have been modified 5 minutes ago:



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()))


issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.



Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).



My SQL knowledge is home made by trial and errors and step by step learning.



Thanks for your nice cooperation.










share|improve this question
























  • I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
    – Nick.McDermaid
    Nov 12 at 0:49















up vote
0
down vote

favorite












I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.



Then I query google.maps with the addresses and collect the driving distance in km.



If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.



The following code works fine:



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


Now I try to make it work to update all entries that have been modified 5 minutes ago:



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()))


issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.



Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).



My SQL knowledge is home made by trial and errors and step by step learning.



Thanks for your nice cooperation.










share|improve this question
























  • I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
    – Nick.McDermaid
    Nov 12 at 0:49













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.



Then I query google.maps with the addresses and collect the driving distance in km.



If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.



The following code works fine:



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


Now I try to make it work to update all entries that have been modified 5 minutes ago:



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()))


issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.



Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).



My SQL knowledge is home made by trial and errors and step by step learning.



Thanks for your nice cooperation.










share|improve this question















I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.



Then I query google.maps with the addresses and collect the driving distance in km.



If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.



The following code works fine:



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


Now I try to make it work to update all entries that have been modified 5 minutes ago:



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()))


issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.



Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).



My SQL knowledge is home made by trial and errors and step by step learning.



Thanks for your nice cooperation.







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:31









fewlinesofcode

1,876515




1,876515










asked Nov 11 at 13:40









Frédéric Morreale

11




11












  • I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
    – Nick.McDermaid
    Nov 12 at 0:49


















  • I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
    – Nick.McDermaid
    Nov 12 at 0:49
















I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
– Nick.McDermaid
Nov 12 at 0:49




I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
– Nick.McDermaid
Nov 12 at 0:49

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249323%2fupdate-statement-with-variables-and-answer-coming-from-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249323%2fupdate-statement-with-variables-and-answer-coming-from-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly